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.
npm install jspdfVerified import paths — ran on the pinned version, not inferred.
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'.
Ensure your target browser environment is modern (not IE) or use appropriate polyfills if compatibility with very old browsers is essential, though not officially supported.
To enable file system access, use Node.js's `--permission` flag or set `jsPDF.allowFsRead` property to `true`. Exercise caution and validate all file paths if enabling this feature: `const doc = new jsPDF(); doc.allowFsRead = true;`
Always use the latest stable version of jsPDF. Critically, **sanitize all user-provided input** before passing it to any jsPDF method, especially `text`, `html`, `addImage`, `addSvgAsImage`, and form-related functions. Consider using DOMPurify explicitly if you are handling untrusted HTML input.
Ensure `dompurify` is installed as a dependency and that you are using a recent version of jsPDF (v4.2.1 or newer is recommended) to benefit from the latest security patches for HTML rendering.
Use named import: `import { jsPDF } from 'jspdf';` for ESM or `const { jsPDF } = require('jspdf');` for CommonJS.Ensure 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.
Ensure 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.
If 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.**