PhantomJs – How to render a multi page PDF

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

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Just need to specify a paperSize.

Like this with module “phantom”: “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);
}

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

PS: I use async.waterfall in this example

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

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply