jsPDF
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
-
TypeError: jsPDF is not a constructor
cause Attempting to instantiate `jsPDF` using a default import or incorrect CommonJS `require` statement, when it is exported as a named export.fixUse named import: `import { jsPDF } from 'jspdf';` for ESM or `const { jsPDF } = require('jspdf');` for CommonJS. -
Error: fs.readFileSync is not a function
cause This error can occur in browser environments if you are trying to use features of the Node.js specific build (e.g., saving to local file paths directly without browser download prompts) or if the build tool incorrectly bundles the Node.js version.fixEnsure your build system is configured to use the browser-compatible UMD or ES builds (e.g., `jspdf.umd.min.js` or `jspdf.es.min.js`). If in Node.js, ensure `jspdf` is resolving to its `jspdf.node.js` variant. -
SecurityError: The operation is insecure.
cause When trying to save a PDF in a browser environment, this can happen if the browser's security policies (e.g., related to iframes or sandboxed environments) prevent file downloads or local storage operations.fixEnsure your application is served over HTTPS and not running in a highly restricted sandbox. Test in a standard browser environment. Some browser extensions might also interfere with file downloads. -
UnhandledPromiseRejectionWarning: Error: Path traversal detected. Access to file system denied.
cause This warning/error occurs in Node.js environments with jsPDF v4.0.0+ when an operation attempts to access the file system without explicit permission, due to the new security restrictions.fixIf you genuinely need file system access, set `jsPDF.allowFsRead = true;` after `const doc = new jsPDF();` or enable Node.js `--permission` flag. **Only do this if you fully trust the file paths being accessed.**
Warnings
- breaking jsPDF v3.0.0 officially dropped support for Internet Explorer. Code relying on IE-specific features or older JavaScript environments may break.
- breaking In Node.js builds, v4.0.0 introduced a critical path traversal/local file inclusion fix. File system access is now restricted by default. Attempts to read local files via paths outside explicit allowances will fail.
- gotcha jsPDF has had numerous security vulnerabilities related to PDF Object Injection, HTML Injection, JavaScript Execution, and Denial of Service (DoS) in various modules (AcroForm, addImage, addJS, output methods) across versions 3.0.1, 3.0.2, 4.1.0, 4.2.0, and 4.2.1.
- gotcha Starting with v3.0.0, the `html` function relies on an updated `dompurify` dependency (v3.2.4+). Older versions of `dompurify` (or not having it installed) may leave `html` function susceptible to XSS vulnerabilities. Later versions (v4.1.0 and v4.2.x) also explicitly upgrade `dompurify` due to further vulnerabilities.
Install
-
npm install jspdf -
yarn add jspdf -
pnpm add jspdf
Imports
- jsPDF
import jspdf from 'jspdf'; import { default as jsPDF } from 'jspdf';import { jsPDF } from 'jspdf'; - jsPDF
const jsPDF = require('jspdf');const { jsPDF } = require('jspdf'); - jsPDF
const jsPDF = window.jspdf;
const { jsPDF } = window.jspdf;
Quickstart
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!');