npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nodepdf-series

v1.2.0

Published

Convert files or urls to PDF.

Downloads

17

Readme

Build Status

nodepdf-series

Render a list of pages to PDF:

var PDF = require('nodepdf-series');

PDF(['http://httpbin.org', 'http://httpbin.org/get'], function (err) {
  if (err) {
    throw err;
  } else {
    console.log('httpbin.org.pdf and httpbin.org/get.pdf created');
  }
});

Render to a specified path and/or file names:

var PDF = require('nodepdf-series');

PDF(['http://httpbin.org', 'http://httpbin.org/get'],
    { outPath: 'testPath', fileNames: ['file1', 'file2'] },
    function (err) {
      if (err) throw err;
      // testPath/file1.pdf and testPath/file2.pdf created
    });

Works with local files too. This renders all HTML-files in subdirectories of current path:

var glob = require('glob');
var path = require('path');
var PDF = require('nodepdf-series');

glob('**/*.html', function (error, files) {
  if (error) {
    throw error;
  }
  // append file:// and resolve path to files
  files = files.map(file => 'file://' + path.resolve(file));
  // render them
  PDF(files, function (err) {
    if (err) {
      throw err;
    } else {
      // now PDF-files should live beside HTML-files, for example:
      // path/to/file1.html and path/to/file1.pdf
      console.log('done');
    }
  });
});

Installation

npm install nodepdf-series

API

Signature:

PDF(pages, callback);
PDF(pages, options, callback);

pages is an array of urls. options is an object with properties:

  • outPath - Which directory to store PDFs. If not set, the current directory and the URL path will be used.
  • fileNames - An array of filenames to use. If not set, last part of URL will be used.
  • args - Arguments for the PhantomJS process.
  • Properties the PhantomJS page accept, like viewportSize.

callback is called when the rendering is done.

Default options

var defaults = {
  viewportSize: {
    // this should be equal to paper size
    width: 1050,
    height: 1485
  },
  paperSize: {
    /**
     * A4 ratio in millimeters: 210 x 297
     * DPI is hardcoded 72 in phantomJS.
     * A resolution of 1050px will give 1050 / 72 * 25.4 ~ 370 mm width,
     * which is way much larger than A4. Most printers will handle this,
     * and scale correctly to given paper source.
     */
    width: 1050,
    height: 1485,
    orientation: 'portrait',
    margin: '1cm'
  },
  args: '',
  captureDelay: 100
};

Performance

nodepdf-series spawns only one child of phantomjs, giving some extra performance compared to nodepdf. Here is a test on 95 local html files:

$ time node nodepdf.js
real    9m32.928s
user    5m56.769s
sys     0m56.142s

$ time node nodepdf-series.js
real    2m47.053s
user    1m55.567s
sys     0m5.104s

You may also want to spread load on all CPU-cores:

var cpus = require('os').cpus().length;
var chunk = require('lodash.chunk');
var each = require('async-each');

var files = [
  'path/to/file1.html', 'path/to/file5.html', 'path/to/file9.html',
  'path/to/file2.html', 'path/to/file6.html', 'path/to/file10.html',
  'path/to/file3.html', 'path/to/file7.html', 'path/to/file11.html',
  'path/to/file4.html', 'path/to/file8.html', 'path/to/file12.html',
];

var chunkSize = Math.round(files.length / cpus);
var chunks = chunk(files, chunkSize);

each(chunks, function (chunk, cb) {
  PDF(chunk, cb);
}, function (err) {
  if (err) {
    console.error('Something went wrong.');
    console.error(err);
  } else {
    // now PDF-files should live beside HTML-files, for example:
    // path/to/file1.html and path/to/file1.pdf
    console.log('done');
  }
});