node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »



NPM Version License Build Status

node.js phantom wrapper for converting html to pdf in scale

Yet another implementation of html to pdf conversion in node.js using phantomjs. This one differs from others in performance and scalability. Unlike others it allocates predefined number of phantomjs worker processes which are then managed and reused using FIFO strategy. This eliminates phantomjs process startup time and it also doesn't flood the system with dozens of phantomjs process under load.

var fs = require('fs')
var conversion = require("phantom-html-to-pdf")();
conversion({ html: "<h1>Hello World</h1>" }, function(err, pdf) {
  var output = fs.createWriteStream('/path/to/output.pdf')
    // since is a node.js stream you can use it
    // to save the pdf to a file (like in this example) or to
    // respond an http request.;

Installation troubleshooting

  • windows works out of the box.

  • macOS sierra update works only with phantomjs2, see below

  • linux may need to additionally install fontconfig package
    sudo yum install -y fontconfig
    sudo apt-get install -y libfontconfig

Global options

var conversion = require("phantom-html-to-pdf")({
    /* number of allocated phantomjs processes */
    numberOfWorkers: 2,
    /* timeout in ms for html conversion, when the timeout is reached, the phantom process is recycled */
    timeout: 5000,
    /* directory where are stored temporary html and pdf files, use something like npm package reaper to clean this up */
    tmpDir: "os/tmpdir",
    /* optional port range where to start phantomjs server */
    portLeftBoundary: 1000,
    portRightBoundary: 2000,
    /* optional hostname where to start phantomjs server */
    host: '',
    /* use rather dedicated process for every phantom printing
      dedicated-process strategy is quite slower but can solve some bugs
      with corporate proxy */
    strategy: "phantom-server | dedicated-process",
    /* optional path to the phantomjs binary
       NOTE: When using phantomjs 2.0, be aware of */
    phantomPath: "{path to phantomjs}",
    /* see phantomjs arguments for proxy setting details */
    /* the collected console.log messages are trimmed by default */
    maxLogEntrySize: 1000

Local options

    html: '<h1>Hello world</h1>',
    header: '<h2>This is the header</h2>',
    footer: '<div style="text-align:center">{#pageNum}/{#numPages}</div>',
    url: "",//set direct url instead of html
    printDelay: 0,//time in ms to wait before printing into pdf
    waitForJS: true,//set to true to enable programmatically specify (via Javascript of the page) when the pdf printing starts (see Programmatic pdf printing section for an example)
    waitForJSVarName: //name of the variable that will be used as a printing trigger, defaults to "PHANTOM_HTML_TO_PDF_READY" (see Programmatic pdf printing section for an example)
    allowLocalFilesAccess: false,//set to true to allow request starting with file:///
    // see PhantomJS options for paperSize -
    paperSize: {
        format, orientation, margin, width, height, headerHeight, footerHeight
   fitToPage: false, //whether to set zoom if contents don't fit on the page
    customHeaders: [],
        cookies: [{
                name: 'cookie-name',
                value: 'cookie-value',
                path: '/',
                domain: ''//Leave blank when working on localhost - "." will get prepended to domain
    injectJs: [], // injects javascript files in the page
    settings: {
        javascriptEnabled : true,
        resourceTimeout: 1000
    // see phantomjs docs -
    viewportSize: {
        width: 600,
        height: 600
    format: {
        quality: 100
}, cb);


This package includes phantomjs 1.9.x distribution. If you like to rather use latest phantomjs you can provide it in the phantomPath option.

Install phantomjs-prebuilt and then...

var conversion = require("phantom-html-to-pdf")({
    phantomPath: require("phantomjs-prebuilt").path
    html: "foo",   
}, function (err, res){});

Kill workers

//kill all phantomjs workers when using phantom-server strategy

Header and footer

It is possible to specify a custom header and a custom footer using HTML.

In the header and footer there is no access to the rest of the document and thus styling with classes and leveraging external CSS does not work. Only inline styles works. This is a limitation on the current PhantomJS implementation.

To print page numbers, use the directives {#pageNum} and {#numPages}, respectively to add current page number and total number of pages. For example:

<div style='text-align:center'>{#pageNum}/{#numPages}</div>

It's also possible to use JavaScript. But note that the JavaScript code has no access to the rest of the HTML document either. Here is an example to modify the paging start:

<span id='pageNumber'>{#pageNum}</span>
    var elem = document.getElementById('pageNumber');
    if (parseInt(elem.innerHTML) <= 3) { = 'none';

Programmatic pdf printing

If you need to programmatic trigger the pdf printing process (because you need to calculate some values or do something async in your page before printing) you can enable the waitForJS local option, when waitForJS is set to true the pdf printing will wait until you set a variable to true in your page, by default the name of the variable is PHANTOM_HTML_TO_PDF_READY but you can customize it via waitForJSVarName option.


local options:

    html: "<custom html here>",
    waitForJS: true,
    viewportSize: {
        width: 600,
        height: 600
    format: {
        quality: 100
}, cb);

custom html:

    // do some calculations or something async
    setTimeout(function() {
        window.PHANTOM_HTML_TO_PDF_READY = true; //this will start the pdf printing
    }, 500);

Image in header

To be able to display an image in the header or footer you need to add the same image to the main content and hide it with style="display:none".

Further notes

You may find some further information and usage examples in the jsreport documentation or try pdf printing in the online playground.


See license