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 🙏

© 2026 – Pkg Stats / Ryan Hefner

pdfile

v1.1.0

Published

PDFile is a Node.js library for generating high-quality, dynamic PDFs using Handlebars templates and Puppeteer. It supports multi-page PDFs and offers full customization. Perfect for web developers, it enables easy creation of multi-page PDFs by simply de

Downloads

132

Readme

PDFile

PDFile is a lightweight Node.js library for generating high-quality, dynamic PDFs from Handlebars templates using Puppeteer. It supports multi-page PDFs, true streaming output, and a caller-owned browser lifecycle suitable for servers and long-running processes.

Installation

npm i pdfile

Requires Node.js 18+. Puppeteer downloads a matching Chromium on install.

Quick start

import { generatePdf, closeBrowser } from 'pdfile';

await generatePdf({
  templateFilePath: 'templates/invoice.hbs',
  dataPerPage: [{ invoiceNumber: 1, total: '$100' }],
  pdfFilePath: 'out/invoice.pdf',
});

// Always release Chromium when you're done with the singleton API.
await closeBrowser();

API

generatePdf(options) — singleton

Uses a module-level Browser. Convenient for scripts and one-shot use. Call closeBrowser() when finished so the Node process can exit.

| Option | Type | Required | Default | Description | | ------------------- | ------------------------------- | -------------------- | -------------------------------------------------- | ---------------------------------------------------------------------------------------- | | templateFilePath | string | Yes | — | Path to the Handlebars template file. | | dataPerPage | Array<object> | Yes | — | One object per page. Each entry renders the template once; outputs are concatenated. | | pdfFilePath | string | If useStream false | — | Where to write the generated PDF. | | useStream | boolean | No | false | Return a Readable PDF stream instead of writing to disk. Mutually exclusive with pdfFilePath. | | helpers | Record<string, Function> | No | — | Custom Handlebars helpers as a plain object. Preferred over helpersFilePath. | | helpersFilePath | string | No | — | Deprecated. Path to a JSON file of stringified function bodies; loaded via new Function. | | puppeteerOptions | LaunchOptions | No | { headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'] } | Puppeteer launch options (only honored on the first call for the singleton). | | pdfOptions | PDFOptions | No | A4, 10mm margins, printBackground: true | Merged on top of the defaults. |

PdfGenerator — caller-owned lifecycle

Each instance owns its own Browser. Use this in servers, tests, or any context where you don't want a global. Multiple instances can run in parallel.

import { PdfGenerator } from 'pdfile';

const gen = new PdfGenerator({ headless: 'new' });
try {
  await gen.generate({
    templateFilePath: 'templates/invoice.hbs',
    dataPerPage: [data],
    pdfFilePath: 'out/invoice.pdf',
  });
} finally {
  await gen.close();
}
  • new PdfGenerator(launchOptions?) — Puppeteer launch options.
  • warmup() — pre-launch Chromium (optional).
  • generate(options) — same option shape as generatePdf (without puppeteerOptions).
  • close() — shut down the owned Browser.

Streaming output

When useStream: true, generate() returns a Puppeteer-native PDF Readable (via page.createPDFStream()) — the PDF is not buffered into memory.

import { createWriteStream } from 'fs';
import { pipeline } from 'stream/promises';

const stream = await generatePdf({
  templateFilePath: 'templates/invoice.hbs',
  dataPerPage: [data],
  useStream: true,
});

await pipeline(stream, createWriteStream('out/invoice.pdf'));

Custom helpers

Prefer the object form — safe, type-checked, and visible to your bundler:

await gen.generate({
  templateFilePath,
  dataPerPage,
  pdfFilePath,
  helpers: {
    money: (n: number) => `$${n.toFixed(2)}`,
    upper: (s: string) => s.toUpperCase(),
  },
});

The legacy helpersFilePath JSON format ({ "name": "function(a){ return a; }" }) is still accepted but deprecated — values are passed through new Function, so anyone who can write to that file can run code in your process.

Multi-page templates

Each entry in dataPerPage renders the template once and the outputs are concatenated. For paginated output, include a page break at the bottom of your template:

<section style="page-break-after: always">
  <!-- one page of content -->
</section>

Built-in Handlebars helpers

| JavaScript | Standard helper | ifCond block helper | | --------------- | ---------------------- | ------------------------------- | | a === b | {{#if (eq a b)}} | {{#ifCond a '===' b}} | | a !== b | {{#if (ne a b)}} | {{#ifCond a '!==' b}} | | a > b | {{#if (gt a b)}} | {{#ifCond a '>' b}} | | a >= b | {{#if (gte a b)}} | {{#ifCond a '>=' b}} | | a < b | {{#if (lt a b)}} | {{#ifCond a '<' b}} | | a <= b | {{#if (lte a b)}} | {{#ifCond a '<=' b}} | | a && b | {{#if (and a b)}} | {{#ifCond a '&&' b}} | | a \|\| b | {{#if (or a b)}} | {{#ifCond a '\|\|' b}} |

Working example

See example/file.service.ts for a runnable invoice example. The generated PDF:

plot

License

MIT