A PDF generation library for Node.js
PDFKit is a mature PDF generation library that provides low-level control over document creation in Node.js and browser environments. Unlike HTML-to-PDF converters, it offers a canvas-like API where you explicitly position text, draw vector shapes, embed fonts, and add images using coordinates and styling commands. This approach mirrors native PDF construction, giving developers precise control over every element's placement and appearance.
The library was built to solve the problem of generating complex, print-ready PDFs programmatically without relying on browser rendering engines or external services. With over 1.8 million weekly downloads, it's widely used in applications that need to produce invoices, reports, tickets, certificates, and other structured documents where layout consistency and visual precision matter. PDFKit's streaming architecture means you can generate large documents without loading everything into memory, making it suitable for server-side batch processing.
PDFKit stands out for its font subsetting capabilities through fontkit, which automatically includes only the glyphs you use to keep file sizes small. It supports TrueType, OpenType, WOFF, and font collections, along with advanced typography features. The library also handles PDF-specific functionality like encryption, access privileges, AcroForms for fillable PDFs, and accessibility standards (PDF/UA). While it requires more manual work than template-based solutions, this control makes it the go-to choice when you need pixel-perfect output or complex graphics that can't be easily expressed in HTML.
const PDFDocument = require('pdfkit');
const fs = require('fs');
const doc = new PDFDocument({ size: 'A4', margin: 50 });
doc.pipe(fs.createWriteStream('invoice.pdf'));
doc.fontSize(20).text('INVOICE #12345', { align: 'right' });
doc.moveDown();
doc.fontSize(10)
.text('Bill To:', 50, 150)
.font('Helvetica-Bold')
.text('Acme Corporation', 50, 165)
.font('Helvetica')
.text('123 Business St', 50, 180)
.text('New York, NY 10001', 50, 195);
const tableTop = 250;
doc.fontSize(10).font('Helvetica-Bold');
doc.text('Item', 50, tableTop);
doc.text('Quantity', 250, tableTop);
doc.text('Price', 350, tableTop, { align: 'right' });
doc.text('Total', 450, tableTop, { align: 'right' });
doc.moveTo(50, tableTop + 15).lineTo(550, tableTop + 15).stroke();
doc.font('Helvetica');
const items = [
{ name: 'Widget Pro', qty: 2, price: 99.99 },
{ name: 'Support Package', qty: 1, price: 249.00 }
];
let y = tableTop + 25;
items.forEach(item => {
doc.text(item.name, 50, y);
doc.text(item.qty.toString(), 250, y);
doc.text(`$${item.price.toFixed(2)}`, 350, y, { align: 'right' });
doc.text(`$${(item.qty * item.price).toFixed(2)}`, 450, y, { align: 'right' });
y += 20;
});
const total = items.reduce((sum, item) => sum + (item.qty * item.price), 0);
doc.font('Helvetica-Bold')
.text('TOTAL:', 350, y + 20, { align: 'right' })
.text(`$${total.toFixed(2)}`, 450, y + 20, { align: 'right' });
doc.end();Invoice and receipt generation: E-commerce platforms and SaaS applications use PDFKit to create branded invoices with precise table layouts, embedded logos, and QR codes. The streaming API allows generating thousands of invoices in batch jobs without memory issues.
Custom reports and dashboards: Business intelligence tools generate PDF reports with charts, graphs, and formatted data tables. PDFKit's vector graphics support enables drawing custom visualizations that scale cleanly when printed.
Event tickets and badges: Ticketing systems create PDFs with barcodes, variable text positioning, and background images. Font embedding ensures tickets display correctly regardless of the recipient's installed fonts.
Legal and regulatory documents: Financial services generate compliance documents with specific formatting requirements, digital signatures, and encryption. PDFKit's access control features restrict printing or copying when needed.
Certificates and diplomas: Educational platforms produce personalized certificates with custom fonts, borders, and seal images. The coordinate-based positioning ensures consistent layout across thousands of generated documents.
npm install pdfkitpnpm add pdfkitbun add pdfkit