{"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install pdf-creator-node"],"cli":null},"imports":["import pdf from 'pdf-creator-node';","import type { PdfDocument } from 'pdf-creator-node';","import type { PdfCreateOptions } from 'pdf-creator-node';"],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}