发布于 2015-08-18 20:53:48 | 1472 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的PhantomJS教程,程序狗速度看过来!

PhantomJS

PhantomJS 是一个基于WebKit的服务器端 JavaScript API。它全面支持web而不需浏览器支持,其快速,原生支持各种Web标准: DOM 处理, CSS 选择器, JSON, Canvas, 和 SVG。PhantomJS可以用于页面自动化,网络监测,网页截屏,以及无界面测试等。


开篇

最近使用 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

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

1 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‘ }

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

zoomFactor :页面缩放比例

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务