Library / SDK
foliojs/pdfkit avatar
foliojs/pdfkit

PDFKit: JavaScript PDF Generation for Node and the Browser

A JavaScript PDF generation library for Node and the browser

10,710 stars1,231 forksJavaScriptMIT

At a glance

What is it?
PDFKit is a JavaScript library that generates PDF documents programmatically in Node.js and in the browser, providing a chainable API for vector graphics, text layout, font embedding, image embedding, tables, AcroForms, and PDF security. Version 0.20.2 was released on 2026-08-30.
Who is it for?
PDFKit is a solid choice for Node.js services that generate reports, invoices, or documents in PDF format and need control over typography, layout, and embedding. The chainable API is easy to start with, and the font subsetting, AcroForms support, and Tagged PDF output cover a wide range of document types.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository received new commits within the last day.
What is it written in?
Mainly JavaScript, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on September 30, 2026, and from our analysis. They are not legal advice.

Editorial analysis

What PDFKit Does and Who Uses It

PDFKit generates PDF documents from JavaScript code. It is not an HTML-to-PDF converter. Instead of taking HTML input, it provides a JavaScript API for placing text, drawing vector paths, embedding images, setting fonts, and adding annotations. The library's README describes the design goal as making complex, multi-page, printable documents as simple as a few function calls.

The primary users are Node.js services that generate invoices, reports, certificates, or other structured documents programmatically. Browser use is supported, with some additional configuration needed for standard fonts. The library runs in both environments from the same codebase.

Version 0.20.2 was released on 2026-08-30. The repository has had active recent releases: v0.20.1 on 2026-08-23 and v0.20.0 on the same day. The last push was on 2026-09-24.

Installing PDFKit and Creating a First Document

PDFKit is published on npm as `pdfkit`:

bash
npm install pdfkit

Or with Yarn:

bash
yarn add pdfkit

The README shows a basic example that creates a one-page document, embeds a font, adds text, embeds an image, and writes the output to a file:

javascript
const { PDFDocument } = require('pdfkit');
const fs = require('fs');

// Create a document
const doc = new PDFDocument();

// Pipe its output somewhere, like to a file or HTTP response
// See below for browser usage
doc.pipe(fs.createWriteStream('output.pdf'));

// Embed a font, set the font size, and render some text
doc
  .font('fonts/PalatinoBold.ttf')
  .fontSize(25)
  .text('Some text with an embedded font!', 100, 100);

The API is chainable: most methods return the document instance, so you can chain `.font()`, `.fontSize()`, and `.text()` in sequence. Call `doc.end()` to finalise the document and flush it to the pipe.

Both `const PDFDocument = require('pdfkit')` and `import PDFDocument from 'pdfkit'` remain supported. The README recommends the named `PDFDocument` export for new code to ease a future migration to ESM.

Browser Usage: toBlob, toBytes, and Bundler Pitfalls

PDFKit supports three paths for browser use: Browserify, Webpack, and a prebuilt standalone file. The prebuilt file is distributed as `pdfkit.standalone.js` in releases and in the package's `js/` folder.

The README documents two experimental output helpers imported from `pdfkit/output`. `toBlob` returns a Promise that resolves to a Blob suitable for creating object URLs in browsers:

javascript
import { PDFDocument, registerStdFonts } from 'pdfkit';
import Helvetica from 'pdfkit/standard-fonts/Helvetica';
import HelveticaBold from 'pdfkit/standard-fonts/HelveticaBold';
import { toBlob } from 'pdfkit/output';

registerStdFonts(Helvetica, HelveticaBold);
const doc = new PDFDocument();
const output = toBlob(doc);

The bundler pitfall documented in the README applies to all environments, not just browsers. The Node.js build loads standard font metrics from `standard-fonts/` and PDF/A sRGB profile data from `data/`, both resolved relative to the pdfkit package directory. A bundle that inlines pdfkit does not sit in that directory, so the relative paths fail at runtime. The README gives three workarounds: mark pdfkit as external and ship `node_modules/pdfkit` alongside the bundle; copy `js/standard-fonts/` next to an ESM bundle; or register fonts explicitly with `registerStdFonts()` before use.

Text Layout, Fonts, and Font Subsetting

PDFKit supports line wrapping with soft hyphen recognition, text alignment (left, right, center, justified), and bulleted lists. The text alignment and list support are listed as features in the README and cover the most common document layout needs.

Font embedding supports TrueType (.ttf), OpenType (.otf), WOFF, WOFF2, TrueType Collections (.ttc), and Datafork TrueType (.dfont) files. The README points to fontkit, a separate library in the foliojs organisation, for details on advanced glyph layout support. Font subsetting means only the glyphs actually used in the document are embedded, which keeps file sizes manageable for documents that use a fraction of a large typeface.

The 14 standard PDF fonts (Helvetica, Times, Courier, and variants) are available without a custom font file. In Node.js, their metrics are loaded from `standard-fonts/` next to the pdfkit package. In browser builds, they must be registered explicitly with `registerStdFonts()`.

Vector Graphics, Images, Tables, and Annotations

PDFKit exposes a vector graphics API modelled on the HTML5 Canvas. The feature list includes path operations, transformations, linear and radial gradients, and an SVG path parser for creating paths from SVG path data strings.

Image embedding supports JPEG and PNG files, including indexed PNGs and PNGs with transparency. The `doc.image()` call accepts local file paths in Node.js or data URIs in the browser.

Tables are a listed feature in the README. AcroForms allow the creation of fillable PDF form fields. Annotations include links, notes, highlights, underlines, and other markup types.

PDF security is supported through encryption and access privilege control. The README lists specific privileges: printing, copying, modifying, annotating, form filling, content accessibility, and document assembly. These correspond to the PDF specification's permission flags.

Tagged PDF and accessibility support is listed as a feature, covering marked content, logical structure, and PDF/UA output for accessibility compliance.

PDF/A Output and Accessibility

PDFKit supports PDF/A output, which is the archiving standard required for long-term document preservation and some regulatory filing requirements. PDF/A output requires embedding the sRGB ICC colour profile, which PDFKit reads from `data/sRGB_IEC61966_2_1.icc` relative to `import.meta.url`. The README notes that CommonJS bundles cannot use this path because CommonJS does not have `import.meta.url`; calling PDF/A output from a CommonJS bundle will produce an error.

The Tagged PDF feature covers the accessibility output that screen readers and assistive technologies use to read document structure. The README lists marked content and logical structure as supported capabilities. Tagged PDF is described under accessibility support as covering PDF/UA, which is the ISO standard for universally accessible PDF documents.

PDFKit's security model uses the `@noble/ciphers` and `@noble/hashes` packages (listed in package.json dependencies) for encryption. The access privilege flags map to PDF specification permission bits: printing, copying, modifying, annotating, form filling, content accessibility, and document assembly. Setting these flags at document creation time allows serving PDFs from a server that restrict what end users can do with the downloaded file, though these restrictions are enforced only by compliant PDF readers.

For outlines, PDFKit supports PDF bookmarks that appear in the reader's navigation panel. Outlines are a separate feature from annotations; they provide the hierarchical table of contents visible in the outline panel of Acrobat or similar readers.

The examples directory in the repository shows usage for several scenarios: `examples/kitchen-sink.js` and `examples/kitchen-sink-accessible.js` demonstrate a wide range of features together, `examples/form.js` shows AcroForms usage, and `examples/accessible-links.js` shows Tagged PDF with links. Each example includes a corresponding pre-generated `.pdf` output file for comparison.

Limitations and Alternatives

PDFKit does not render HTML. If your requirement is to convert an existing HTML page or template to PDF, PDFKit is not the right tool. Puppeteer, which drives a headless Chromium browser, renders HTML and exports it as PDF, preserving CSS layout. wkhtmltopdf uses the WebKit engine for the same purpose. Both are different in kind from PDFKit.

The experimental `toBlob` and `toBytes` helpers are explicitly marked as unstable in the README, meaning their interface may change before stabilisation. Code that depends on them should pin the pdfkit version.

PDFKit does not currently support automatic multi-column text layout or table reflow across pages. The README lists tables as a supported feature, but complex table layouts that overflow to a second page require managing page breaks in the calling code. The higher-level layout API is listed as planned but not yet available.

PDFMake is another JavaScript PDF generation library that takes a document definition object as JSON. The authoring model is different from PDFKit's imperative API: PDFMake is declarative and may be easier to use for simple table-heavy documents, while PDFKit provides lower-level control over positioning and path drawing. The MIT licence applies to both libraries. PDFKit's package.json lists version 0.20.2 as the current release.

Editorial conclusion

PDFKit is a solid choice for Node.js services that generate reports, invoices, or documents in PDF format and need control over typography, layout, and embedding. The chainable API is easy to start with, and the font subsetting, AcroForms support, and Tagged PDF output cover a wide range of document types. It is not the right tool when you need to render existing HTML to PDF: PDFKit generates PDF from its own API calls, not from HTML. For HTML-to-PDF, tools like Puppeteer or wkhtmltopdf are different solutions. Check the bundling section before using PDFKit inside a Webpack, Rollup, or esbuild build: standard fonts load from the file system relative to the pdfkit package directory, and a bundle that inlines pdfkit will not find them automatically.

Frequently asked questions

Can I use PDFKit in React?

Yes. PDFKit runs in the browser, including React applications. You need to use a bundler like Webpack or Vite and register standard fonts with `registerStdFonts()` before creating a document. The `toBlob` helper from `pdfkit/output` is the recommended way to produce a Blob for display or download.

how to use pdfkit in node js

Install pdfkit with `npm install pdfkit`, create a `PDFDocument` instance, pipe it to a file stream with `doc.pipe(fs.createWriteStream('output.pdf'))`, call methods to add text, images, and graphics, then call `doc.end()` to write the output.

pdfkit vs puppeteer

PDFKit generates PDFs programmatically from a JavaScript API; you build the document using code calls for text, images, and vector graphics. Puppeteer renders HTML with a headless Chromium browser and exports the rendered page as PDF. PDFKit is for generating documents from data; Puppeteer is for converting existing HTML pages.

pdfkit vs pdfmake

PDFKit provides an imperative JavaScript API where you call methods to place text, draw paths, and embed images. PDFMake uses a declarative document definition object. PDFKit gives lower-level control over layout and graphics; PDFMake may be simpler for table-heavy structured documents.

Official sources

  1. foliojs/pdfkit on GitHub
  2. License: MIT
  3. Project website
  4. README
  5. Releases
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/foliojs-pdfkit.svg)](https://hysenlabs.com/projects/foliojs-pdfkit)