Generating PDF Reports in JavaScript (Node.js): Puppeteer, PDFKit, jsPDF

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Generating PDF Reports in JavaScript (Node.js): Puppeteer, PDFKit, jsPDF



Creating PDF documents is one of the most common tasks in web development. Reports, invoices, certificates, documentation — all of this requires a reliable tool for generating files on the server side. In the Node.js ecosystem, there are several powerful libraries, each solving the task in its own way. In this article, we will cover three main approaches: browser emulation via Puppeteer, low-level PDF manipulation via PDFKit, and the client-server solution jsPDF.



1. Puppeteer — Generating PDF from HTML/CSS



Puppeteer is a library for controlling a headless Chromium browser. It allows you to load any HTML page, apply styles, and "screenshot" it into a PDF. This is an ideal choice if you already have report layouts in HTML/CSS and want an accurate transfer to PDF.



Key Features:

  • Support for all CSS properties (Flexbox, Grid, fonts, shadows).
  • Works with JavaScript on the page (Chart.js charts, dynamic tables).
  • Configuration of format, margins, headers/footers, backgrounds.
  • Support for custom fonts (TTF, OTF).


Example of generating a report:



const puppeteer = require('puppeteer');

async function generateReport() { const browser = await puppeteer.launch(); const page = await browser.newPage();

// Load HTML directly (or by URL) await page.setContent(` <html> <head> <style> body { font-family: Arial; padding: 40px; } h1 { color: #2c3e50; } .data { background: #ecf0f1; padding: 20px; } </style> </head> <body> <h1>Sales Report</h1> <div class="data"> <p>Revenue: 1,200,000 RUB</p> <p>Number of orders: 345</p> </div> </body> </html> `, { waitUntil: 'networkidle0' });

await page.pdf({ path: 'report.pdf', format: 'A4', printBackground: true, margin: { top: '20mm', bottom: '20mm' } });

await browser.close(); console.log('PDF created!');}

generateReport();


Pros: layout accuracy, support for complex layouts, works with charts.

Cons: high memory consumption (Chromium weighs ~200 MB), slow startup.



2. PDFKit — Low-Level PDF Creation



PDFKit is a library for direct manipulation of the PDF structure. You don't use HTML; instead, you build the document programmatically: drawing lines, adding text, inserting images. This gives you full control over every pixel of the document.



Key Features:

  • Vector graphics (lines, rectangles, curves).
  • Unicode support and font embedding.
  • Creating tables, page numbering.
  • Working with annotations and links.


Example of creating a simple invoice:



const PDFDocument = require('pdfkit');const fs = require('fs');

const doc = new PDFDocument({ size: 'A4', margin: 50 });doc.pipe(fs.createWriteStream('invoice.pdf'));

// Headerdoc.fontSize(24).text('Invoice No. 12345', { align: 'center' });doc.moveDown();

// Table (manually)doc.fontSize(12).text('Description', 50, 150);doc.text('Qty', 350, 150);doc.text('Price', 450, 150);

doc.moveTo(50, 170).lineTo(550, 170).stroke();

doc.text('Development Services', 50, 180);doc.text('1', 350, 180);doc.text('150,000 RUB', 450, 180);

doc.moveDown(2);doc.fontSize(16).text('Total: 150,000 RUB',

Recommendations