jsPDF

4.2.1 · active · verified Sun Apr 19

jsPDF is a client-side JavaScript library for generating PDF documents. The current stable version is 4.2.1, with frequent patch and minor releases primarily addressing security vulnerabilities and bug fixes, indicating an active maintenance and development cadence. It enables developers to create PDFs directly in the browser or Node.js environment, supporting various paper sizes, orientations, and units (e.g., millimeters, inches). Key differentiators include its pure JavaScript nature, allowing it to run without server-side dependencies, and its robust API for adding text, images, and other content. It bundles different module formats (ESM, UMD, Node) to support diverse environments, often requiring no explicit path specification in imports as build tools handle it. The project has recently focused heavily on fixing various security-related issues, emphasizing the importance of sanitizing all user input.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart code demonstrates how to initialize a jsPDF document, add basic text and shapes, and save the resulting PDF file. It uses the default A4 portrait format and millimeters for units, then outputs 'my-first-document.pdf'.

import { jsPDF } from 'jspdf';

// Create a new PDF document with default settings (A4, portrait, millimeters)
const doc = new jsPDF();

// Add text to the document
doc.text('Hello, jsPDF World!', 10, 10); // 'Hello, jsPDF World!' at x=10mm, y=10mm

// Add another line of text with a different font size
doc.setFontSize(16);
doc.text('This is a test document generated by jsPDF.', 10, 20);

// Add a rectangle (x, y, width, height, style)
doc.rect(10, 30, 50, 20, 'S'); // 'S' for stroke

// Save the document, triggering a download in browsers or writing to file in Node.js
doc.save('my-first-document.pdf');

console.log('PDF document generated and saved!');

view raw JSON →