PDF-LIB

Create and modify PDF documents in any JavaScript environment

README

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.
  
    <img
      src="https://img.shields.io/npm/v/pdf-lib.svg?style=flat-square"
      alt="NPM Version"
    />
  
    <img
      src="https://img.shields.io/circleci/project/github/Hopding/pdf-lib/master.svg?style=flat-square&label=CircleCI"
      alt="CircleCI Build Status"
    />
  
    <img
      src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square"
      alt="Prettier Badge"
    />
  
    <img
      src="https://img.shields.io/static/v1?label=discord&message=pdf-lib&color=566fbb&style=flat-square"
      alt="Discord Badge"
    />

Learn more at pdf-lib.js.org


Table of Contents


  - Fill Form

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 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._



  1. ``` js
  2. import { PDFDocument, StandardFonts, rgb } from 'pdf-lib'

  3. // Create a new PDFDocument
  4. const pdfDoc = await PDFDocument.create()

  5. // Embed the Times Roman font
  6. const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman)

  7. // Add a blank page to the document
  8. const page = pdfDoc.addPage()

  9. // Get the width and height of the page
  10. const { width, height } = page.getSize()

  11. // Draw a string of text toward the top of the page
  12. const fontSize = 30
  13. page.drawText('Creating PDFs in JavaScript is awesome!', {
  14.   x: 50,
  15.   y: height - 4 * fontSize,
  16.   size: fontSize,
  17.   font: timesRomanFont,
  18.   color: rgb(0, 0.53, 0.71),
  19. })

  20. // Serialize the PDFDocument to bytes (a Uint8Array)
  21. const pdfBytes = await pdfDoc.save()

  22. // For example, `pdfBytes` can be:
  23. //   • Written to a file in Node
  24. //   • Downloaded from the browser
  25. //   • Rendered in an