Phantomjs 生成多页PDF

开篇css

最近使用 Phantomjs 生成PDF,其中遇到一些问题,致使PDF生成失败,如出现空白文件或一页数据量太大,都是因为没有设置好格式致使。特别是分页问题,感受资料不多,除了在 StackOverflow 上看到些许资料外,中文社区基本看不到,附上修改后的 rasterize.js 来作讲解:html

 1 var page = require('webpage').create(),
 2     system = require('system'),
 3     address, output, size;
 4 
 5 if (system.args.length < 3 || system.args.length > 5) {
 6     console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]');
 7     console.log('  paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
 8     phantom.exit(1);
 9 } else {
10     address = system.args[1];
11     output = system.args[2];
12     /*size of browser*/
13     page.viewportSize = { width: 600, height: 600 };
14     /*
15     if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") {
16         size = system.args[3].split('*');
17         page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: '0px' }
18                                            : { format: 'A4', orientation: 'portrait', margin: '1cm' };
19     }
20     */
21     /* ie and chrome view diffrent format of pdf */
22     page.settings.userAgent = 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36';
23     page.paperSize = { format: 'A4', orientation: 'portrait', margin: '0.8cm' };
24     page.zoomFactor = 1;
25     page.settings.loadImages = true;
26     //some question about the page language
27     page.open(address, function (status) {
28         if (status !== 'success') {
29             console.log('Unable to load the address!');
30         } else {
31             //page.render(output);
32             //phantom.exit();
33             
34             window.setTimeout(function () {
35                 page.render(output);
36                 phantom.exit();
37             }, 200); //setting the time is enough to loading the page. document.ready
38             
39         }
40     });
41 }
View Code

 

PDF 格式设置git

关于其中 page 的设置属性,这里能够了解,更深刻能够了解 WebPage Modulegithub

咱们须要的设置,基本上就是页面格式、缩放、加载图片等,但有些例外,下面一一讲解。web

1 page.paperSize = { format: 'A4', orientation: 'portrait', margin: '0.8cm' };

注释掉了官方例子的设置代码,由于传入的参数只有3个,到 .pdf 为止,若是写成通用模式,固然能够做为外部参数传入。ajax

format :A4 纸,能够设置 "5in*7.5in", "10cm*20cm",  "Letter" 等chrome

orientation :纸方向是竖着的,或者 landscapeapi

margin :与纸四边间距,可自定义,也可详细设置 margin : { left: '0.8cm',  top : '0.8cm',  right : '0.8cm',  bottom : '0.8cm' }ide

1 page.zoomFactor = 1;
2 page.settings.loadImages = true;

zoomFactor :页面缩放比例ui

loadImages :页面加载图片

1 page.settings.userAgent = 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36';

这个设置比较不常见,通常的示例中都没有说起,由于发现用 chrome 和 IE 打开生成的 pdf 时格式有点不同(表如今分页方面),因为偏向 Chrome 浏览格式,故设置此值,解决这个不一致问题。 

page.open 里面的 setTimeout 方法做用:等待页面执行完 js ,再生成 pdf。固然对于 js 要执行多久(要等多久),这个就不知道怎么预算了。其实我有试过 ajax 方式加载内容,但所以问题而做罢了。

更多的信息,关于页眉和页脚及页码标注问题,能够参考这里

 

PDF 分页

分页来讲,更好控制,不须要代码(js)设置,页面使用样式便可:

style = “page-break-after: always;”

控制每页内容的大小,使用 <div style="page-break-after: always;">content</div> 就行。

更多选择 style=“page-break-before: always;” , style="page-break-inside: avoid;" 这个能够避免内容散到两页中

 

总结

关于这个 phantomjs pdf render 就到此了,若有更多好的方式及问题解决方案,欢迎你们分享。

相关文章
相关标签/搜索