开篇

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

 var page = require('webpage').create(),
system = require('system'),
address, output, size; if (system.args.length < 3 || system.args.length > 5) {
console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]');
console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
phantom.exit(1);
} else {
address = system.args[1];
output = system.args[2];
/*size of browser*/
page.viewportSize = { width: 600, height: 600 };
/*
if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") {
size = system.args[3].split('*');
page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: '0px' }
: { format: 'A4', orientation: 'portrait', margin: '1cm' };
}
*/
/* ie and chrome view diffrent format of pdf */
page.settings.userAgent = 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36';
page.paperSize = { format: 'A4', orientation: 'portrait', margin: '0.8cm' };
page.zoomFactor = 1;
page.settings.loadImages = true;
//some question about the page language
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
} else {
//page.render(output);
//phantom.exit(); window.setTimeout(function () {
page.render(output);
phantom.exit();
}, 200); //setting the time is enough to loading the page. document.ready }
});
}

PDF 格式设置

关于其中 page 的设置属性,这里可以了解,更深入可以了解 WebPage Module

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

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

注释掉了官方例子的设置代码,因为传入的参数只有3个,到 .pdf 为止,如果写成通用模式,当然可以作为外部参数传入。

format :A4 纸,可以设置 "5in*7.5in", "10cm*20cm",  "Letter" 等

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

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

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

zoomFactor :页面缩放比例

loadImages :页面加载图片

 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 就到此了,如有更多好的方式及问题解决方案,欢迎大家分享。

Phantomjs 生成多页PDF的更多相关文章

  1. iTextSharp动态生成多页pdf及追加内容等记录

    1.要动态生成pdf,无非是用第三方或直接代码生成. 2.iTextSharp生成pdf问题点记录 dll相关下载 https://files.cnblogs.com/files/xlgwr/iTex ...

  2. java 多页pdf转化为多张图片

    相关jar包: <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian ...

  3. 将div的内容生成清晰的PDF、高清PDF

    //需要引入html2canvas.js.jquery.js文件 html: <button type="button" class="btn btn-primar ...

  4. ASP.NET MVC 解析模板生成静态页一(RazorEngine)

    简述 Razor是ASP.NET MVC 3中新加入的技术,以作为ASPX引擎的一个新的替代项.在早期的MVC版本中默认使用的是ASPX模板引擎,Razor在语法上的确不错,用起来非常方便,简洁的语法 ...

  5. EasyUI中动态生成标签页

    这是最近学到的内容,当时是有思路但是不知道怎么获取当前的点击对象,就没有实现功能,通过更深入的学习,我知道了不仅仅是Java,Oracle中有一个this,同样的EasyUI中也存在一个this,来获 ...

  6. 基于PHP生成静态页的实现方法

    t1.php 复制代码 代码如下: <?php// 方法一根据模版生成静态页面// replaceTemplateString函数用于替换模板中指定字符串function replaceTemp ...

  7. 【phantomjs】使用phantomjs生成highChart的图片(待完善)

    阅读目录 //center }, subtitle: { text: 'Source: WorldClimate.com', x: -20 }, xAxis: { categories: ['Jan' ...

  8. 生成静态页面的PHP类

    生成静态页面的PHP类: 复制代码代码如下: <?php   class html   {    var $dir; //dir for the htmls(without/)    var $ ...

  9. mvc分页生成静态页,mvc生成静态页

    http://blog.csdn.net/xxj_jing/article/details/7899125 分页生成静态页 http://www.cnblogs.com/luanyilin/archi ...

随机推荐

  1. C# EF 基础操作

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. border做三角符号

    用border做三角符号以及其他图形 ;; border-width:20px 10px; border-style:solid; border-color:#ff3300 #ff3300 #ffff ...

  3. 机器学习入门-贝叶斯统计语料库的词频.groupby() collections

    1..groupby()[].agg(by={}) 2. collections.de...(lambda:1) 统计的单词是语料库中所有的词, 对Dataframe统计单词词频,同时增加一列数据co ...

  4. as3 string split方法一些注意

    split () 方法 AS3 function split(delimiter:*, limit:Number = 0x7fffffff):Array 如果指定 limit 参数,返回的数组中具有的 ...

  5. Apache Hive 执行HQL语句报错 ( 10G )

    # 故障描述: hive > , ) as uuid, count(distinct(request_body["uuid"])) as count from log_bft ...

  6. hibernate自带的注解和jpa注解的冠希

    hibernate是实现了JPA规范,在我们使用hibernate框架的时候,我们引入了hibernate3或者4这个核心包.hibernate-jpa-2.0-api-1.0.0.Final.jar ...

  7. How to Pronounce the Word ‘TO’

    How to Pronounce the Word ‘TO’ Share Tweet Share Tagged With: TO Reduction Study the TO reduction.   ...

  8. Delphi 动态数组合并

    TIntArray = array of Integer; function MergeArray(const ArrayA, ArrayB: TIntArray): TIntArray; var i ...

  9. webpack 常用插件及作用

    copy-webpack-plugin :复制文件到目标文件夹.在开发时使用热模替换,(没有生成dist 文件夹,都在内存中),如果想引用某一个js文件,直接写script标签是找不到的,因为服务器内 ...

  10. Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:generate (default-cli) on project : <properties> resource does not exist

    使用mybatis-generator自动生成mapper.dao等文件时,报错如下: org.apache.maven.lifecycle.LifecycleExecutionException: ...