PhantomJs-如何呈现多页PDF [英] PhantomJs - How to render a multi page PDF

查看:303
本文介绍了PhantomJs-如何呈现多页PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用phantomJS创建一页PDF;但是我在文档上找不到如何创建不同的页面(每个页面都来自html视图)并将它们放入一个PDF中的方法?我正在为NodeJS使用node-phantom模块

I can create one-page PDFs with phantomJS; but I can't find on the doc how to create different pages (each page coming from an html view) and put them into one PDF ? I am using node-phantom module for NodeJS

推荐答案

只需指定paperSize.

使用模块幻影":"0.5.1"

function(next) {
    phantom.create(function(doc) {
        next(null, doc);
    }, "phantomjs", Math.floor(Math.random()*(65535-49152+1)+49152));
},
function(ph, next) {
    ph.createPage(function(doc) {
        next(null, doc);
    });
},
function(page, next) {
    page.set('paperSize', {format: 'A4', orientation: 'portrait'});
    page.set('zoomFactor', 1);
}

然后,每次要打开新页面时,只需在HTML内容中使用 page-break-before:always; .

Then, simply use page-break-before: always; in your HTML content each time you want to open a new page.

PS:在此示例中,我使用async.waterfall

PS: I use async.waterfall in this example

PPS:端口号上的Math.random用于避免模块崩溃,如果同时调用幻影二进制文件被触发.效果很好-如果有人想发布更好的内容,即使有些偏离主题,也可以随时这样做

PPS: Math.random on port number is used to avoid module crash if concurrent calls to phantom binary are triggered. Works fine - if someone wants to post something better even if a bit off-topic, feel free to do it

这篇关于PhantomJs-如何呈现多页PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆