pdf-lib
Create and modify PDF documents in any JavaScript environment
pdf-lib is a JavaScript/TypeScript library for creating new PDF files and editing existing ones from code, running in Node.js, browsers, Deno, and React Native, one of the few JS libraries that can modify an existing PDF.
pdf-lib is a JavaScript library for creating new PDF files and editing existing ones, directly from code. It is designed to work in any JavaScript environment: Node.js on a server, a web browser, Deno, and React Native on mobile. Most other JavaScript PDF tools can only generate new documents; pdf-lib is one of the few that also lets you open and modify a PDF that already exists.
The list of things you can do with it is broad. On the document level you can add pages, insert pages at specific positions, remove pages, or copy pages from one PDF into another. On a page level you can draw text in various fonts, draw images (PNG and JPEG), embed vector graphics defined in SVG paths, and draw other PDF pages as embedded content. The library also supports form fields: you can create new interactive forms with text fields, checkboxes, radio buttons, dropdowns, and signature fields, fill them programmatically, or flatten them (which locks the values in as static content). Custom fonts can be embedded, including fonts that support Unicode character sets for non-English text.
Beyond visible content, the library lets you set and read document metadata (title, author, creation date, etc.) and viewer preferences (how the PDF should open in a reader, which pages to show, etc.). You can also add file attachments to a PDF.
The API is promise-based and works with JavaScript's async/await pattern. Creating a basic PDF takes fewer than 20 lines of code, as shown in the README examples, which also demonstrate modifying an existing file, creating a form, and embedding images. Interactive demos are hosted on JSFiddle for trying the examples without any local setup.
The library is MIT licensed and written in TypeScript, which means it ships with type definitions for use in TypeScript projects. The full README is longer than what was shown.
Where it fits
- Generate a PDF report with custom text, embedded images, and custom fonts from a Node.js script.
- Fill out an existing PDF form programmatically, set text fields, checkboxes, and radio buttons, then flatten to lock values.
- Copy pages from multiple PDF files and merge them into a single combined document.
- Add a watermark or header text to every page of an existing PDF without re-creating it from scratch.