Generating PDF Reports in JavaScript (Node.js): Library Overview and Code Examples

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): A Complete Guide



Creating PDF documents is one of the most in-demand tasks in web development. Whether it's exporting invoices, generating analytical reports, or creating certificates — you need a reliable tool. In the Node.js ecosystem, there are several powerful libraries that allow you to solve this task quickly and flexibly. In this article, we will break down three main approaches and show you how to choose the right tool for your project on PythonLib.



Why Generate PDF on the Server?

Client-side generation (browser) is often limited: the user may lose data when refreshing the page, and mobile devices do not always correctly handle complex layouts. The server-side approach on Node.js gives you full control over the document structure, protects business logic, and allows you to integrate generation into CI/CD pipelines.



Top 3 Libraries for Working with PDF in Node.js



1. PDFKit — Low-Level Content Manipulation

PDFKit is the most popular library for creating PDFs from scratch. It does not require installing additional dependencies (like Chromium) and allows you to control every pixel of the document.

  • Pros: lightweight (just a few megabytes), complete layout freedom, support for fonts, images, vector graphics.
  • Cons: requires writing a lot of code for complex layouts, no automatic text wrapping.

Example of creating a simple invoice:

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

const doc = new PDFDocument({ margin: 50 });

doc.pipe(fs.createWriteStream('invoice.pdf'));

// Headerdoc.fontSize(25).text('Invoice #123', { align: 'center' });doc.moveDown();

// Tabledoc.fontSize(12).text('Product: Consultation', { continued: true }) .text('Price: 5000 RUB', { align: 'right' });

doc.moveDown();doc.text('Total: 5000 RUB', { align: 'right' });

doc.end();


2. Puppeteer — Generation from HTML/CSS

Puppeteer launches a headless Chromium browser and converts an HTML page to PDF. This is an ideal choice if you already have HTML templates with CSS styles.

  • Pros: simple layout via HTML, support for all CSS properties (flex, grid, @media print), ability to add headers and footers.
  • Cons: heavy dependency (~300 MB Chromium), slow startup, not suitable for simple documents.

Example of converting HTML to PDF:

const puppeteer = require('puppeteer');

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

await page.setContent(`<h1 style="color: blue;">Report for March 2025</h1> <p>Total Revenue: <strong>1,200,000 RUB</strong></p>`);

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

await browser.close();}

generateReport();


3. jsPDF — Universal Tool (Client + Server)

jsPDF is a library that works both in the browser and in Node.js. It provides high-level methods for creating tables, charts, and even embedding fonts.

  • Pros: simple API, built-in table support (via plugin), small size.
  • Cons: on the server requires manual font configuration (not all Cyrillic characters are available by default).

Example with a table:

const { jsPDF } = require("jspdf");require("jspdf-autotable");

const doc = new jsPDF();

doc.text("Sales Statistics", 14, 20);

doc.autoTable({ head: [['Month', 'Sales', 'Profit']], body: [ ['January', '100', '20,000'],

Recommendations