PDF Generation in JavaScript (Node.js): Libraries and Tools

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

PDF Generation in JavaScript (Node.js): Libraries and Tools



Generating PDF documents is one of the common tasks in web development. Invoices, reports, certificates, tickets — all of these are conveniently created on the server side. In the Node.js ecosystem, there are several mature solutions, each suitable for specific scenarios. In this article, we will look at three main approaches and popular libraries for creating PDFs in JavaScript.



Overview of Popular Libraries



The choice of tool depends on the complexity of the document: simple text reports, data tables, or complex layouts with HTML+CSS. Three key libraries are presented below.



1. PDFKit — Low-Level PDF Manipulation



PDFKit is the most popular library for creating PDFs "from scratch." It allows you to draw text, lines, rectangles, and images using a coordinate grid. It is ideal for dynamic documents with precise positioning.



Advantages of PDFKit:

  • Full control over every page element
  • Does not require a browser or Puppeteer
  • Support for embedding fonts (TrueType, OpenType)
  • Works asynchronously with streams (Stream API)


Example of a simple document:



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

const doc = new PDFDocument();doc.pipe(fs.createWriteStream('output.pdf'));

// Titledoc.fontSize(25).text('Hello, PDFKit!', 100, 100);

// Regular textdoc.fontSize(12).text('This is a simple PDF document created on Node.js.', 100, 150);

// Draw a linedoc.moveTo(100, 200).lineTo(500, 200).stroke();

// Add an imagedoc.image('logo.png', 100, 220, { width: 100 });

doc.end();


2. Puppeteer — Generation from HTML/CSS



Puppeteer is a library for controlling a headless Chrome browser. It allows you to load an HTML page (or generate it on the fly) and save it as a PDF. This is the best choice if you already have HTML templates or complex CSS styles.



Advantages of Puppeteer:

  • Accurate CSS rendering (flex, grid, fonts)
  • Support for JavaScript on the page (charts, tables)
  • Ability to set format, margins, headers and footers
  • Ideal for reports and invoices


Example of generating a PDF from HTML:



const puppeteer = require('puppeteer');

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

const html = `<!DOCTYPE html> <html> <head><style> body { font-family: Arial; padding: 20px; } h1 { color: #2c3e50; } table { border-collapse: collapse; width: 100%; } td, th { border: 1px solid #ccc; padding: 8px; } </style></head> <body> <h1>Monthly Report</h1> <table> <tr><th>Product</th><th>Quantity</th></tr> <tr><td>Laptop</td><td>5</td></tr> <tr><td>Mouse</td><td>10</td></tr> </table> </body></html>`;

await page.setContent(html); await page.pdf({ path: 'report.pdf', format: 'A4', printBackground: true });

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


3. jsPDF — Client-Side (and Node.js) Generation



jsPDF was originally created for the browser but has a version for Node.js. It is lightweight and well-suited for simple documents. In Node.js, it is used via the jspdf package with additional dependencies.



Advantages of jsPDF:

  • Small size
  • Support for Unicode and Cyrillic (via font loading)
  • Can work both on the client and

Recommendations