Generating PDF Reports in JavaScript (Node.js)
Creating PDF documents is one of the most in-demand tasks in web development. Reports, invoices, certificates, documentation — all of this requires a reliable tool for server-side generation. In the Node.js ecosystem, there are several powerful libraries that allow you to flexibly generate PDF files. In this article, we will thoroughly examine three main approaches: PDFKit (direct programming), Puppeteer (HTML rendering), and jsPDF (lightweight option).
Why generate PDF on the server?
- Dynamic content — data from databases, calculations, charts.
- Security — access control and versioning.
- Automation — mass distribution of reports on a schedule.
- Unified format — correct display on all devices.
1. PDFKit — Full Control Over the Document
PDFKit is the most popular library for creating PDFs "from scratch". It does not require a browser and works exclusively with JavaScript objects. You control every element: text, images, vector graphics, fonts.
Installation
npm install pdfkitExample: Simple Report with a Table
const PDFDocument = require('pdfkit');const fs = require('fs');
const doc = new PDFDocument();
doc.pipe(fs.createWriteStream('report.pdf'));
// Titledoc.fontSize(24).text('Monthly Report', { align: 'center' });doc.moveDown();
// Subtitledoc.fontSize(14).text('Period: January 2025', { align: 'center' });doc.moveDown(2);
// Table (simulation)doc.fontSize(12).font('Helvetica-Bold');doc.text('Product', 50, 150);doc.text('Quantity', 250, 150);doc.text('Price', 400, 150);
doc.font('Helvetica');const data = [ { item: 'Laptops', qty: 15, price: 75000 }, { item: 'Monitors', qty: 30, price: 25000 }, { item: 'Keyboards', qty: 100, price: 1500 }];
let y = 170;data.forEach(row => { doc.text(row.item, 50, y); doc.text(String(row.qty), 250, y); doc.text(String(row.price), 400, y); y += 20;});
// Totaldoc.moveDown();doc.font('Helvetica-Bold');doc.text(`Total: ${data.reduce((sum, r) => sum + r.qty * r.price, 0)} RUB`, 50, y + 20);
doc.end();PDFKit Capabilities:
- Vector graphics (lines, rectangles, circles).
- Font embedding (TrueType, OpenType).
- Unicode and Cyrillic support.
- Creating links and annotations.
2. Puppeteer — Rendering HTML to PDF
If you already have an HTML report template (e.g., using Bootstrap), Puppeteer is the ideal choice. It launches a headless Chrome browser and converts the page to PDF, preserving all styles, fonts, and JavaScript animations.
Installation
npm install puppeteerExample: Converting an HTML Template
const puppeteer = require('puppeteer');
async function generatePDF() { const browser = await puppeteer.launch(); const page = await browser.newPage();
// HTML with CSS const html = ` <!DOCTYPE html> <html> <head> <style> body { font-family: Arial; padding: 40px; } h1 { color: #2c3e50; } table { width: 100%; border-collapse: collapse; } th, td { border: 1px solid #ccc; padding: 8px; } </style> </head> <body> <h1>Sales Report</h1> <table> <tr><th>Month</th><th>Amount</th></tr> <tr><td>January</td><td>450,000 RUB</td>