Guide · Open source
How to use jsPDF: generate PDFs in JavaScript
Updated · written with AI from the project's own documentation and checked against our catalogue data
jsPDF is an open-source JavaScript library for generating PDF documents from code. You create a document object, add content such as text, then save or download the file. This guide covers installing it, making a first PDF, changing the page setup, running it in Node.js, and handling non-ASCII text.
Quick start
- 1Install jsPDF. Run npm install jspdf --save or yarn add jspdf in your project folder.
- 2Import the library. Import { jsPDF } from "jspdf" in your module, or require it in Node.js.
- 3Create a document. Call new jsPDF() for the default A4 portrait page, or pass options for orientation, unit and format.
- 4Add content. Call doc.text with a string and x and y coordinates.
- 5Save or download. Call doc.save with a file name. In Node.js the file is written to the current working directory.
- 6Add a custom font if needed. For UTF-8 text beyond ASCII, add a .ttf font with addFileToVFS and addFont, then select it with setFont.
Install jsPDF with npm or yarn
The recommended way to get jsPDF is from npm. You can also load it with a script tag from unpkg if you are not using a package manager.
The package's dist folder contains several builds. Build tools and Node pick the right file automatically, so importing "jspdf" is usually enough. The README does not state a required Node.js version.
- jspdf.es.*.js: ES2015 module format.
- jspdf.node.*.js: for Node, using file operations to load and save files.
- jspdf.umd.*.js: UMD format for AMD or script-tag loading.
npm install jspdf --save
# or
yarn add jspdfCreate and save your first PDF
Import jsPDF, create a document, add text at a position, and save it. The default page is A4, portrait, with millimeters as units.
The text call takes the string and x and y coordinates. The save call downloads the file in a browser.
import { jsPDF } from "jspdf";
const doc = new jsPDF();
doc.text("Hello world!", 10, 10);
doc.save("a4.pdf");Change paper size, orientation and units
Pass an options object to the constructor to set orientation, unit and format. The example below creates a landscape page that is 2 by 4 inches.
Coordinates are then measured in the unit you chose, so the text position here is in inches.
const doc = new jsPDF({
orientation: "landscape",
unit: "in",
format: [4, 2]
});
doc.text("Hello world!", 1, 1);
doc.save("two-by-four.pdf");Use jsPDF in Node.js
When you require jsPDF in Node.js, it loads the Node version automatically. Calling save writes the file to the current working directory.
Other module formats are also supported: AMD, and globals loaded through window.jspdf.
const { jsPDF } = require("jspdf");
const doc = new jsPDF();
doc.text("Hello world!", 10, 10);
doc.save("a4.pdf");Handle security and user input
The README strongly advises sanitizing user input before passing it to jsPDF. In Node.js, jsPDF restricts reading files from the local file system by default.
The README recommends Node's permission flags over the jsPDF.allowFsRead setting, because the runtime enforces the flags. Note that all imported JavaScript files and dependencies must be included in --allow-fs-read.
- Sanitize user input before adding it to a document.
- Prefer node --permission --allow-fs-read=... over jsPDF.allowFsRead.
- The allowFsRead setting is a fallback and is marked not recommended.
node --permission --allow-fs-read=... ./scripts/generate.jsAdd UTF-8 text with a custom font
The 14 standard PDF fonts only cover the ASCII codepage. For other characters, such as Chinese, add a custom .ttf font that contains the glyphs you need. If the font lacks them, the output shows garbled characters.
You can convert a .ttf file with the project's fontconverter, or load the file as a binary string with fetch or XMLHttpRequest and register it with addFileToVFS and addFont.
- Check that your font has the glyphs your text needs.
- Use the fontconverter at /fontconverter/fontconverter.html in the project docs to create a JavaScript file from a .ttf.
const doc = new jsPDF();
const myFont = ... // load the *.ttf font file as binary string
// add the font to jsPDF
doc.addFileToVFS("MyFont.ttf", myFont);
doc.addFont("MyFont.ttf", "MyFont", "normal");
doc.setFont("MyFont");What people use it for
- Letting users download a generated PDF from a web page.
- Writing a PDF file from a Node.js script.
- Building invoices, reports or certificates from data in your app.
- Generating PDFs in the browser so you do not need a server-side PDF service.
Frequently asked questions
Is jsPDF free to use?
jsPDF is licensed under the MIT licence, as stated in the project information. Check the LICENSE file in the repository for the full terms.
How do I install jsPDF?
Run npm install jspdf --save or yarn add jspdf. You can also load the library with a script tag from unpkg, using the jspdf.umd.min.js file.
Does jsPDF work in Node.js?
Yes. Requiring jsPDF in Node.js loads the Node build, and doc.save writes the file to the current working directory. The README does not state a minimum Node version.
Why does my UTF-8 or Chinese text show as garbled characters?
The standard PDF fonts only support ASCII. Add a .ttf font that includes the glyphs you need, using the fontconverter or addFileToVFS and addFont, then call setFont.
Can jsPDF edit or fill existing PDF files?
No. The README only describes generating new documents. If you need to edit existing PDFs, look at a different library.
What are the alternatives to jsPDF?
Projects listed as comparable include pdfmake, pdf-lib, PDFKit and Puppeteer. Pick based on whether you need client-side generation, editing existing files, or rendering HTML.
New agent skills, repos and AI tools — every week
The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.
Our verdict
jsPDF suits web and Node.js developers who want to generate new PDFs from code without a server-side service. It is a code library with no interface, so non-developers should look elsewhere, and it is a poor fit for editing existing PDFs or for non-Latin text without adding a custom font.
Install options & downloads