有一个新的 jQuery + 云解决方案可以将任何 HTML 页面及其 CSS(包括打印媒体规则)呈现为 PDF。解决方案是设置打印网页的任何区域,您只需告诉格式化程序您要打印哪个容器元素,其余的由库完成。你得到的是一个可嵌入的 PDF,否则后端会推回一个 PDF 以供下载。
这是您的库 (GitHub):
https://github.com/Xportability/css-to-pdf
这是你的小提琴:
http://jsfiddle.net/kstubs/jrtM5/
这是一个工作演示:
http://xep.cloudformatter.com/doc/
目前没有使用说明,但跟随示例(查看源代码)应该是不言自明的(查找 Print It 按钮),这里或多或少是 Format 方法的附加选项/参数明白。
options
{
pageWidth: "8.5in",
pageHeight: "11in",
pageMargin: ".25in",
pageMarginTop: "1in",
pageMarginRight: "1in",
pageMarginBottom: "1in",
pageMarginLeft: "1in",
render: ("none"|"newwin<default>"|embed"|"download<default IE>"),
foStyle: {
// puts fo style attributes on the root, ex. fontSize:14px
foStyleName: 'value', ...
}
}
这里是当前支持的 CSS 属性列表。
[
'lineHeight',
'alignmentBaseline',
'backgroundImage', 'backgroundPosition', 'backgroundRepeat', 'backgroundColor',
'baselineShift',
'border',
'borderWidth', 'borderColor','borderStyle',
'borderTop','borderLeft','borderRight','borderBotttom',
'borderTopWidth','borderTopStyle','borderTopColor',
'borderBottomWidth','borderBottomStyle','borderBottomColor',
'borderLeftWidth','borderLeftStyle','borderLeftColor',
'borderRightWidth','borderRightStyle','borderRightColor',
'borderCollapse',
'clear', 'color',
'display', 'direction', 'dominantBaseline',
'fill', 'float',
'fontStyle', 'fontVariant', 'fontWeight', 'fontSize', 'fontFamily',
'listStyleType', 'listStyleImage', 'letterSpacing',
'marginTop', 'marginBottom', 'marginLeft', 'marginRight','orphans',
'paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft',
'pageBreakAfter', 'pageBreakBefore',
'tableLayout',
'textAlign', 'textAnchor','textDecoration', 'textIndent', 'textTransform',
'verticalAlign',
'widows', 'wordSpacing', 'width'
]
希望这会有所帮助!