{"id":11533,"library":"pdf-creator-node","title":"HTML to PDF Converter with Puppeteer","description":"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.","status":"active","version":"4.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/hajareshyam/pdf-creator-node","tags":["javascript","pdf","pdf-generator","html2pdf","html-to-pdf","handlebars","puppeteer","chromium","nodejs","typescript"],"install":[{"cmd":"npm install pdf-creator-node","lang":"bash","label":"npm"},{"cmd":"yarn add pdf-creator-node","lang":"bash","label":"yarn"},{"cmd":"pnpm add pdf-creator-node","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core rendering engine (headless Chromium) for PDF generation. Automatically installed.","package":"puppeteer","optional":false}],"imports":[{"note":"While CommonJS `require` works, TypeScript users should prefer ESM `import` for better type inference and modern module practices. `pdf` is the default export.","wrong":"const pdf = require('pdf-creator-node');","symbol":"pdf","correct":"import pdf from 'pdf-creator-node';"},{"note":"Used for type-checking the document object structure. Important for TypeScript projects.","symbol":"PdfDocument","correct":"import type { PdfDocument } from 'pdf-creator-node';"},{"note":"Used for type-checking the PDF creation options. Essential for robust TypeScript usage.","symbol":"PdfCreateOptions","correct":"import type { PdfCreateOptions } from 'pdf-creator-node';"}],"quickstart":{"code":"import pdf from \"pdf-creator-node\";\nimport fs from \"fs\";\nimport path from \"path\";\n\nconst htmlTemplate = `\n  <!DOCTYPE html>\n  <html>\n    <head>\n      <style>\n        body { font-family: sans-serif; margin: 20mm; }\n        h1 { color: #333; }\n        table { width: 100%; border-collapse: collapse; margin-top: 20px; }\n        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }\n        .footer { position: fixed; bottom: 0; width: 100%; text-align: center; font-size: 10px; color: #666; }\n      </style>\n    </head>\n    <body>\n      <h1>User Report</h1>\n      <table>\n        <thead>\n          <tr>\n            <th>Name</th>\n            <th>Age</th>\n          </tr>\n        </thead>\n        <tbody>\n          {{#each users}}\n          <tr>\n            <td>{{this.name}}</td>\n            <td>{{this.age}}</td>\n          </tr>\n          {{/each}}\n        </tbody>\n      </table>\n      <div class=\"footer\">Page {{page}} of {{pages}}</div>\n    </body>\n  </html>\n`;\n\nconst usersData = [\n  { name: \"Alice\", age: 30 },\n  { name: \"Bob\", age: 24 },\n  { name: \"Charlie\", age: 35 }\n];\n\nconst options = {\n  format: \"A4\",\n  orientation: \"portrait\",\n  border: \"10mm\",\n  header: { height: \"15mm\", contents: { default: '<h2 style=\"text-align: center;\">PDF Report Header</h2>' } },\n  footer: { height: \"10mm\", contents: { default: '<span style=\"color: #444;\">{{page}}</span>/<span>{{pages}}</span>' } },\n  path: path.join(process.cwd(), 'output.pdf') // Use path.join for cross-platform compatibility\n};\n\nconst document = {\n  html: htmlTemplate,\n  data: { users: usersData },\n  path: options.path,\n};\n\npdf.create(document, options)\n  .then((res) => console.log('PDF created:', res.filename))\n  .catch((err) => console.error('Error creating PDF:', err));","lang":"javascript","description":"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."},"warnings":[{"fix":"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`.","message":"Version 4.0.0 replaced PhantomJS with Puppeteer (headless Chromium) for rendering. This results in a larger installation size (hundreds of MB for Chromium download) and potential differences in PDF layout and styling compared to previous versions. Deprecated options like `phantomPath` are now ignored.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Consolidate footer content into a single `default`, `first`, or `last` template. Use `{{page}}` and `{{pages}}` placeholders within that single template for page numbering.","message":"The `footer.contents` option for `PdfCreateOptions` has changed behavior in v4.0.0. Only one template (`default`, `first`, or `last`) is applied; per-page numeric keys (e.g., for page 2) are no longer supported.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"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.","message":"Puppeteer, and by extension `pdf-creator-node` (v4+), downloads a Chromium browser instance upon installation. This can significantly increase `node_modules` size and installation time, particularly in resource-constrained environments or CI/CD pipelines.","severity":"gotcha","affected_versions":">=4.0.0"},{"fix":"Ensure your project's Node.js environment is version 18 or newer.","message":"The minimum Node.js version required is 18. Using older Node.js versions will lead to compatibility issues and errors.","severity":"gotcha","affected_versions":"<4.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Run `npm install pdf-creator-node` or `yarn add pdf-creator-node`.","cause":"The package is not installed or incorrectly referenced.","error":"Error: Cannot find module 'pdf-creator-node'"},{"fix":"Upgrade your Node.js environment to version 18 or higher.","cause":"Running the package with an unsupported Node.js version.","error":"Error: Node.js 18 or newer is required."},{"fix":"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.","cause":"Puppeteer failed to find or launch Chromium, likely due to a failed download or permissions issue during installation, or `PUPPETEER_SKIP_DOWNLOAD` being set without a pre-installed browser.","error":"Error: Failed to launch the browser process! The browser should be installed via 'npm install'"},{"fix":"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.","cause":"Incorrect import or require statement, or accessing an uninitialized module. Possibly trying to destructure `pdf` from a `require` call that returns the default export.","error":"TypeError: pdf.create is not a function"}],"ecosystem":"npm"}