pdf-lib

Create and modify PDF documents in any JavaScript environment.
Designed to work in any modern JavaScript runtime. Tested in Node, Browser, Deno, and React Native environments.

NPM Version CircleCI Build Status Prettier Badge Discord Badge

> **Learn more at [pdf-lib.js.org](https://pdf-lib.js.org)** ## Table of Contents - [Features](#features) - [Motivation](#motivation) - [Usage Examples](#usage-examples) - [Create Document](#create-document) - [Modify Document](#modify-document) - [Create Form](#create-form) - [Fill Form](#fill-form) - [Flatten Form](#flatten-form) - [Copy Pages](#copy-pages) - [Embed PNG and JPEG Images](#embed-png-and-jpeg-images) - [Embed PDF Pages](#embed-pdf-pages) - [Embed Font and Measure Text](#embed-font-and-measure-text) - [Add Attachments](#add-attachments) - [Set Document Metadata](#set-document-metadata) - [Read Document Metadata](#read-document-metadata) - [Set Viewer Preferences](#set-viewer-preferences) - [Read Viewer Preferences](#read-viewer-preferences) - [Draw SVG Paths](#draw-svg-paths) - [Deno Usage](#deno-usage) - [Complete Examples](#complete-examples) - [Installation](#installation) - [Documentation](#documentation) - [Fonts and Unicode](#fonts-and-unicode) - [Creating and Filling Forms](#creating-and-filling-forms) - [Limitations](#limitations) - [Help and Discussion](#help-and-discussion) - [Encryption Handling](#encryption-handling) - [Migrating to v1.0.0](docs/MIGRATION.md) - [Contributing](#contributing) - [Maintainership](#maintainership) - [Tutorials and Cool Stuff](#tutorials-and-cool-stuff) - [Prior Art](#prior-art) - [Git History Rewrite](#git-history-rewrite) - [License](#license) ## Features - Create new PDFs - Modify existing PDFs - Create forms - Fill forms - Flatten forms - Add Pages - Insert Pages - Remove Pages - Copy pages between PDFs - Draw Text - Draw Images - Draw PDF Pages - Draw Vector Graphics - Draw SVG Paths - Measure width and height of text - Embed Fonts (supports UTF-8 and UTF-16 character sets) - Set document metadata - Read document metadata - Set viewer preferences - Read viewer preferences - Add attachments ## Motivation `pdf-lib` was created to address the JavaScript ecosystem's lack of robust support for PDF manipulation (especially for PDF _modification_). Two of `pdf-lib`'s distinguishing features are: 1. Supporting modification (editing) of existing documents. 2. Working in all JavaScript environments - not just in Node or the Browser. There are [other](#prior-art) good open source JavaScript PDF libraries available. However, most of them can only _create_ documents, they cannot _modify_ existing ones. And many of them only work in particular environments. ## Usage Examples ### Create Document _This example produces [this PDF](assets/pdfs/examples/create_document.pdf)._ [Try the JSFiddle demo](https://jsfiddle.net/Hopding/rxwsc8f5/13/) ```js import { PDFDocument, StandardFonts, rgb } from 'pdf-lib' // Create a new PDFDocument const pdfDoc = await PDFDocument.create() // Embed the Times Roman font const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman) // Add a blank page to the document const page = pdfDoc.addPage() // Get the width and height of the page const { width, height } = page.getSize() // Draw a string of text toward the top of the page const fontSize = 30 page.drawText('Creating PDFs in JavaScript is awesome!', { x: 50, y: height - 4 * fontSize, size: fontSize, font: timesRomanFont, color: rgb(0, 0.53, 0.71), }) // Serialize the PDFDocument to bytes (a Uint8Array) const pdfBytes = await pdfDoc.save() // For example, `pdfBytes` can be: // • Written to a file in Node // • Downloaded from the browser // • Rendered in an