Skip to content
/ pdf-lib Public
forked from Hopding/pdf-lib

Create and modify PDF documents in any JavaScript environment

License

Notifications You must be signed in to change notification settings

jerp/pdf-lib

 
 

Repository files navigation

pdf-lib

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

Table of Contents

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 design requirements since its inception are to:

  1. Support modification (editing) of existing documents.
  2. Work 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 (e.g. pdfkit, jspdf, pdfmake). hummus is a NodeJS library capable of both creating and modifying PDF documents. However, hummus is a Node wrapper around a C++ library. This means that it cannot run in all JavaScript environments. For example, you cannot use hummus in the Browser or in React Native.

Features

  • Create new PDFs
  • Modify existing PDFs
  • Add Pages
  • Insert Pages
  • Remove Pages
  • Draw Text
  • Draw Images
  • Draw Vector Graphics
  • Embed Fonts
  • Embed Images

Usage Examples

More detailed examples are available here.

Document Creation

import { PDFDocumentFactory, PDFDocumentWriter, drawText } from 'pdf-lib';

const pdfDoc = PDFDocumentFactory.create();
const [timesRomanFont] = pdfDoc.embedStandardFont('Times-Roman');

const page = pdfDoc
  .createPage([350, 500])
  .addFontDictionary('Times-Roman', timesRomanFont);

const contentStream = pdfDoc.createContentStream(
  drawText('Creating PDFs in JavaScript is awesome!', {
    x: 50,
    y: 450,
    size: 15,
    font: 'Times-Roman',
    colorRgb: [0, 0.53, 0.71],
  }),
);

page.addContentStreams(pdfDoc.register(contentStream));

pdfDoc.addPage(page);

const pdfBytes = PDFDocumentWriter.saveToBytes(pdfDoc);

Document Modification

import { PDFDocumentFactory, PDFDocumentWriter, drawText } from 'pdf-lib';

// This should be a Uint8Array.
// This data can be obtained in a number of different ways.
// If your running in a Node environment, you could use fs.readFile().
// In the browser, you could make a fetch() call and use res.arrayBuffer().
const existingPdfDocBytes = ...

const pdfDoc = PDFDocumentFactory.load(existingPdfDocBytes);
const [helveticaFont] = pdfDoc.embedStandardFont('Helvetica');

const pages = pdfDoc.getPages();
const page  = pages[0];

page.addFontDictionary('Helvetica', helveticaFont);

const contentStream = pdfDoc.createContentStream(
  drawText('This text was added to the PDF with JavaScript!', {
    x: 25,
    y: 25,
    size: 24,
    font: 'Helvetica',
    colorRgb: [0.95, 0.26, 0.21],
  }),
);

page.addContentStreams(pdfDoc.register(contentStream));

const pdfBytes = PDFDocumentWriter.saveToBytes(pdfDoc);

Installation

NPM Module

To install the latest stable version:

# With npm
npm install --save pdf-lib

# With yarn
yarn add pdf-lib

This assumes you're using npm or yarn as your package manager.

UMD Module

You can also download pdf-lib as a UMD module from unpkg. 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:

When using a UMD build, you will have access to a global window.PDFLib variable. This variable contains all of the classes and functions exported by pdf-lib. For example:

// NPM module
import { PDFDocumentFactory, drawText } from 'pdf-lib';

// UMD module
var PDFDocumentFactory = PDFLib.PDFDocumentFactory;
var drawText = PDFLib.drawText;

API Documentation

API documentation is available here.

Prior Art

  • pdfkit is a PDF generation library for Node and the Browser. This library was immensely helpful as a reference and existence proof when creating pdf-lib. pdfkit's code for font embedding, PNG embedding, and JPG embedding was especially useful.
  • jspdf is a PDF generation library for the browser.
  • pdfmake is a PDF generation library for the browser.
  • hummus is a PDF generation and modification library for Node environments. hummus is a Node wrapper around a C++ library.
  • react-native-pdf-lib is a PDF generation and modification library for React Native environments. react-native-pdf-lib is a wrapper around C++ and Java libraries.
  • pdfassembler is a PDF generation and modification library for Node and the browser. It requires some knowledge about the logical structure of PDF document to use.

License

MIT

About

Create and modify PDF documents in any JavaScript environment

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 98.8%
  • Other 1.2%