node.js 如何在 PhantomJS 中处理 PDF 分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17043823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle PDF pagination in PhantomJS
提问by Rayjax
I am using PhantomJS to create PDFs from html.
我正在使用 PhantomJS 从 html 创建 PDF。
It works fine, but I can't find out how to work with pagination; I want to create a page for each div in my document, but I can't find anything in the doc. about pagination.
它工作正常,但我不知道如何使用分页;我想为文档中的每个 div 创建一个页面,但在文档中找不到任何内容。关于分页。
If my document is short, it makes only one page, and if it is bigger, it creates one second empty page and my contents are in the first page which becomes very long.
如果我的文档很短,它只有一页,如果它更大,它会创建第二个空白页,我的内容在第一页,这会变得很长。
Any idea ? (I am using phantomJS-node module for nodeJS)
任何的想法 ?(我正在为 nodeJS 使用 phantomJS-node 模块)
回答by Cybermaxs
PhantomJS takes care of webkit's css implementation. To implement manual page breaks you can use these properties :
PhantomJS 负责 webkit 的 css 实现。要实现手动分页符,您可以使用以下属性:
page-break-before: auto/always/avoid/...page-break-inside: auto/always/avoid/...page-break-after: auto/always/avoid/...
page-break-before:自动/总是/避免/...page-break-inside:自动/总是/避免/...page-break-after:自动/总是/避免/...
For example, a div can be :
例如,一个 div 可以是:
<div style="page-break-before:always;"><!-- content --></div>
or
或者
<div style="page-break-after:always;"> <!-- content --></div>
Controlling page breaks when printing in Webkit is sometimes not easy, in particular with long html tables.
在 Webkit 中打印时控制分页符有时并不容易,尤其是对于长 html 表格。
回答by R. Salisbury
Very late, but I had issues with "break-inside:avoid" using JsReport that were fixed by changing the element's display type to inline-block. More info here: https://github.com/ariya/phantomjs/issues/10638
很晚了,但是我在使用 JsReport 时遇到了“闯入:避免”的问题,这些问题通过将元素的显示类型更改为内联块来解决。更多信息在这里:https: //github.com/ariya/phantomjs/issues/10638
回答by gusgard
You should see this issuewith different tips.
您应该使用不同的提示查看此问题。
Try to use display:inline-blockin the element that you don't want to breaks because the page break. The reasoning behind is that webkit already tries to preserve images from breaking. And images are inline-blocks.
尝试display:inline-block在您不想因为分页符而中断的元素中使用。背后的原因是 webkit 已经尝试保护图像不被破坏。图像是内联块。
回答by sbalcerowski
Pagination works fine with :
分页工作正常:
var page = webPage.create();
page.paperSize = {
format: 'A4',
orientation: 'portrait',
margin: '1cm'
}
Check documentation here http://phantomjs.org/api/webpage/property/paper-size.html
在此处检查文档http://phantomjs.org/api/webpage/property/paper-size.html

