Guide · Open source
How to use pdfkit: install, build a PDF and tips
Updated · written with AI from the project's own documentation and checked against our catalogue data
PDFKit is a JavaScript library that creates PDF files from code. It runs in Node.js and in web browsers, and you build a document by chaining function calls that add text, images, shapes, links and pages. This guide shows you how to install it, create a simple document, and write the result to a file or the browser.
Quick start
- 1Install the package. Run npm install pdfkit or yarn add pdfkit in your project folder.
- 2Create a document. Import PDFDocument and create a new instance with new PDFDocument().
- 3Pipe the output. Connect doc.pipe() to a file stream or HTTP response so the PDF has somewhere to go.
- 4Add content. Chain calls such as font(), fontSize(), text(), image() and addPage() to build the pages.
- 5Finish the file. Call doc.end() so the document is finalized and written out.
Install pdfkit with npm or yarn
PDFKit is installed as a package. The README uses the npm and yarn package managers. The only prerequisite the README names is Node.js; it does not state a required version.
# with npm
npm install pdfkit
# with yarn
yarn add pdfkitCreate your first PDF document
Import the library, create a document, pipe its output to a destination, and call end() when you are finished. Piping sends the output to a file or an HTTP response. The README example writes to output.pdf using Node's fs module.
- Use const { PDFDocument } = require('pdfkit') or the equivalent import; the named PDFDocument export is the one the README recommends for new code.
- Call doc.end() at the end, otherwise the output is not finalized.
const { PDFDocument } = require('pdfkit');
const fs = require('fs');
const doc = new PDFDocument();
doc.pipe(fs.createWriteStream('output.pdf'));
doc.text('Hello from PDFKit');
doc.end();Add text, fonts and images
Chain font() and fontSize() before text() to style it. You can embed a font file such as a TrueType (.ttf) or OpenType (.otf) font. For images, the README lists JPEG and PNG support, including indexed PNGs and PNGs with transparency. The fit option constrains an image to a box, and align and valign center it.
- Supported font formats include TrueType (.ttf), OpenType (.otf), WOFF, WOFF2, TrueType Collections (.ttc) and Datafork TrueType (.dfont).
- Font subsetting is listed as a feature.
doc
.font('fonts/PalatinoBold.ttf')
.fontSize(25)
.text('Some text with an embedded font!', 100, 100);
doc.image('path/to/image.png', {
fit: [250, 300],
align: 'center',
valign: 'center'
});Add pages, vector shapes and links
Call addPage() to start a new page. Vector drawing follows a canvas-like style: you move to a point, draw lines, then fill or stroke. The library also supports SVG path strings, transforms, and linear and radial gradients. Links are added with link() over a rectangle, and underline() can mark the same area.
- Path operations, transformations and gradients are listed under vector graphics.
- Annotations listed in the README include links, notes, highlights and underlines.
doc
.addPage()
.fillColor('blue')
.text('Here is a link!', 100, 100)
.underline(100, 100, 160, 27, { color: '#0000FF' })
.link(100, 100, 160, 27, 'http://google.com/');Use pdfkit in the browser
PDFKit can run in the browser, which avoids a server round trip. The README lists three approaches: Browserify, webpack, or the prebuilt pdfkit.standalone.js file. The browser has no file system access, so you register a Uint8Array under a path before using that path with registerFont, image or file.
To show the result, collect the output as a Blob. The README describes an experimental toBlob helper from pdfkit/output and a stable approach that collects the data chunks from the document's events.
- Experimental helpers toBlob and toBytes come from pdfkit/output and may change.
- Call the helper, or attach the data listeners, before calling doc.end().
const chunks = [];
doc.on('data', chunk => chunks.push(chunk));
doc.on('end', () => {
const blob = new Blob(chunks, { type: 'application/pdf' });
const url = URL.createObjectURL(blob);
iframe.src = url;
});
doc.end();Bundle pdfkit for Node without breaking standard fonts
If you bundle your app with tools such as esbuild, rollup or webpack, pdfkit's standard font metrics may not be found, because they are loaded relative to the built file. The README gives three options: mark pdfkit as external and ship node_modules/pdfkit with the bundle, copy the js/standard-fonts directory next to an ESM bundle, or register the standard fonts before creating the document.
- Using a standard font that is neither registered nor loadable throws an error that names the font.
- PDF/A output reads an sRGB ICC profile from the data directory, which needs the same treatment.
import { PDFDocument, registerStdFonts } from 'pdfkit';
import Helvetica from 'pdfkit/standard-fonts/Helvetica';
registerStdFonts(Helvetica);
const doc = new PDFDocument();What people use it for
- Generating invoices or reports from application data on a Node.js server.
- Creating printable multi-page documents with embedded fonts.
- Producing a PDF in the browser without sending data to a server.
- Adding links and annotations to generated documents.
Frequently asked questions
Is pdfkit free to use?
PDFKit is released under the MIT licence, as listed in the project details. Check the LICENSE file in the repository for the full terms before you ship it.
What do I need to install pdfkit?
You install it with npm install pdfkit or yarn add pdfkit. The README names Node.js as the prerequisite but does not state which Node.js version is required.
Can pdfkit edit an existing PDF?
No. The README describes PDFKit as a tool for creating documents rather than modifying them, so use a different library if you need to fill or edit existing PDFs.
Does pdfkit work in the browser?
Yes. The README lists Browserify, webpack, and a prebuilt pdfkit.standalone.js file as ways to use it. The browser has no file system, so fonts and images must be registered as Uint8Array data.
Which fonts and images can I use?
Fonts can be TrueType, OpenType, WOFF, WOFF2, TrueType Collections or Datafork TrueType. Images can be JPEG or PNG, including indexed PNGs and PNGs with transparency.
How does pdfkit compare with pdfmake or jsPDF?
The README does not compare PDFKit with other libraries. Comparable projects listed in the project details include PDFMake, jsPDF, Puppeteer and pdf-lib, so test the options against your own layout needs.
New agent skills, repos and AI tools — every week
The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.
Our verdict
PDFKit suits Node.js or browser developers who need to generate multi-page, printable PDFs from code, such as invoices or reports. If you need to edit existing PDFs, want a no-code tool, or need the higher-level layout APIs and pattern fills that the README lists as coming soon, look at other options.
Install options & downloads