pdf-creator-node is a Node.js library for converting HTML and Handlebars templates into PDF documents. It leverages Puppeteer, a headless Chromium browser, for rendering, ensuring support for modern CSS features like Flexbox, Grid, and web fonts. As of version 4.0.1, it has moved entirely to Puppeteer, replacing its prior reliance on PhantomJS/html-pdf. This change, introduced in v4.0.0, brings improved rendering fidelity and a more actively maintained underlying engine, though it comes with a larger install footprint due to Chromium. The library is actively maintained, as evidenced by recent v4.0.x releases, and provides a programmatic API to generate reports, invoices, and letters from templated HTML. It balances ease of use for HTML-centric document generation with the resource considerations inherent to a Chromium-based solution, suitable for small to medium-scale production workloads.
npm install pdf-creator-nodeVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create a PDF from an HTML and Handlebars template, including dynamic data, custom headers, and footers, saving it to a file, using ESM syntax.
Review PDF output for layout changes. Account for increased install size, especially in CI/CD environments (consider `PUPPETEER_CACHE_DIR` or `PUPPETEER_SKIP_DOWNLOAD`). Remove PhantomJS-specific options from `PdfCreateOptions`.
Consolidate footer content into a single `default`, `first`, or `last` template. Use `{{page}}` and `{{pages}}` placeholders within that single template for page numbering.For CI/CD, consider setting `PUPPETEER_SKIP_DOWNLOAD=true` if Chromium is pre-installed or `PUPPETEER_CACHE_DIR` to cache the downloaded browser across builds. Be aware of the memory and CPU footprint when running many concurrent PDF generation tasks.
Ensure your project's Node.js environment is version 18 or newer.
Run `npm install pdf-creator-node` or `yarn add pdf-creator-node`.
Upgrade your Node.js environment to version 18 or higher.
Ensure `puppeteer` can download Chromium by checking network access during `npm install`. If `PUPPETEER_SKIP_DOWNLOAD` is set, ensure Chromium is installed and `PUPPETEER_EXECUTABLE_PATH` points to it. Check file permissions for the `node_modules` directory.
For CommonJS, use `const pdf = require('pdf-creator-node');`. For ESM (TypeScript/modern Node.js), use `import pdf from 'pdf-creator-node';` and ensure your environment supports ESM.