Create and modify PDF documents in any JavaScript environment
Create and modify PDF documents in any JavaScript environment
Learn more at pdf-lib.js.org
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:
There are other 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.
This example produces this PDF.
…This example produces this PDF (when this PDF is used for the existingPdfBytes variable).
…This example produces this PDF.
See also Creating and Filling Forms
…This example produces this PDF (when this PDF is used for the formPdfBytes variable, this image is used for the marioImageBytes variable, and this image is used for the emblemImageBytes variable).
See also Creating and Filling Forms
…This example produces this PDF (when this PDF is used for the formPdfBytes variable).
…This example produces this PDF (when this PDF is used for the firstDonorPdfBytes variable and this PDF is used for the secondDonorPdfBytes variable).
…This example produces this PDF (when this image is used for the jpgImageBytes variable and this image is used for the pngImageBytes variable).
…This example produces this PDF (when this PDF is used for the americanFlagPdfBytes variable and this PDF is used for the usConstitutionPdfBytes variable).
…pdf-lib relies on a sister module to support embedding custom fonts: @pdf-lib/fontkit. You must add the @pdf-lib/fontkit module to your project and register it using pdfDoc.registerFontkit(...) before embedding custom fonts.
This example produces this PDF (when this font is used for the fontBytes variable).
…This example produces this PDF (when this image is used for the jpgAttachmentBytes variable and this PDF is used for the pdfAttachmentBytes variable).
…This example produces this PDF.
……This script outputs the following (when this PDF is used for the existingPdfBytes variable):
Title: Microsoft Word - Basic Curriculum Vitae example.doc
Author: Administrator
Subject: undefined
Creator: PScript5.dll Version 5.2
Keywords: undefined
Producer: Acrobat Distiller 8.1.0 (Windows)
Creation Date: 2010-07-29T14:26:00.000Z
Modification Date: 2010-07-29T14:26:00.000Z……This script outputs the following (when this PDF is used for the existingPdfBytes variable):
HideToolbar: true
HideMenubar: true
HideWindowUI: false
FitWindow: true
CenterWindow: true
DisplayDocTitle: true
NonFullScreenPageMode: UseNone
ReadingDirection: R2L
PrintScaling: None
Duplex: DuplexFlipLongEdge
PickTrayByPDFSize: true
PrintPageRange: [ { start: 1, end: 1 }, { start: 3, end: 4 } ]
NumCopies: 2This example produces this PDF.
…pdf-lib fully supports the exciting new Deno runtime! All of the usage examples work in Deno. The only thing you need to do is change the imports for pdf-lib and @pdf-lib/fontkit to use the Skypack CDN, because Deno requires all modules to be referenced via URLs.
See also How to Create and Modify PDF Files in Deno With pdf-lib
Below is the create document example modified for Deno:
import {
PDFDocument,
StandardFonts,
rgb,
} from 'https://cdn.skypack.dev/pdf-lib@^1.11.1?dts';
const pdfDoc = await PDFDocument.create();
const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman);
const page = pdfDoc.addPage();
const { width, height } = page.getSize();
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),
});
const pdfBytes = await pdfDoc.save();
await Deno.writeFile('out.pdf', pdfBytes);If you save this script as create-document.ts, you can execute it using Deno with the following command:
deno run --allow-write create-document.tsThe resulting out.pdf file will look like this PDF.
Here's a slightly more complicated example demonstrating how to embed a font and measure text in Deno:
…If you save this script as custom-font.ts, you can execute it with the following command:
deno run --allow-write --allow-net custom-font.tsThe resulting out.pdf file will look like this PDF.
The usage examples provide code that is brief and to the point, demonstrating the different features of pdf-lib. You can find complete working examples in the apps/ directory. These apps are used to do manual testing of pdf-lib before every release (in addition to the automated tests).
There are currently four apps:
node - contains tests for pdf-lib in Node environments. These tests are a handy reference when trying to save/load PDFs, fonts, or images with pdf-lib from the filesystem. They also allow you to quickly open your PDFs in different viewers (Acrobat, Preview, Foxit, Chrome, Firefox, etc...) to ensure compatibility.web - contains tests for pdf-lib in browser environments. These tests are a handy reference when trying to save/load PDFs, fonts, or images with pdf-lib in a browser environment.rn - contains tests for pdf-lib in React Native environments. These tests are a handy reference when trying to save/load PDFs, fonts, or images with pdf-lib in a React Native environment.deno - contains tests for pdf-lib in Deno environments. These tests are a handy reference when trying to save/load PDFs, fonts, or images with pdf-lib from the filesystem.To install the latest stable version:
# With npm
npm install --save pdf-lib
# With yarn
yarn add pdf-libThis assumes you're using npm or yarn as your package manager.
You can also download pdf-lib as a UMD module from unpkg or jsDelivr. The UMD builds have been compiled to ES5, so they should work in any modern browser. UMD builds are useful if you aren't using a package manager or module bundler. For example, you can use them directly in the <script> tag of an HTML page.
The following builds are available:
**NOT
drawImage not working with big rotated pdf
Turn each page into a picture
Some characters in Japanese (and likely other languages) don't show up when a font is embedded with subset: true
Issue with Jpeg's Orientation Exif when embedding in pdf
removeField() leaves stale widget refs in page /Annots arrays
Corrupted PDF
Flatten is removing RadioGroups and Checkboxes
Expected instance of PDFDict, but got instance of undefined
pdfs with flattened forms can't be printed in Adobe Reader or Acrobat
Unable to display pdf content