explaingit

hopding/pdf-lib

8,443TypeScriptAudience · developerComplexity · 2/5LicenseSetup · easy

TLDR

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.

Mindmap

mindmap
  root((pdf-lib))
    What it does
      Create new PDFs
      Edit existing PDFs
      Cross-platform support
    Features
      Draw text and images
      Form fields
      Page operations
      Custom fonts
    Environments
      Node.js
      Browser
      Deno and React Native
    Audience
      JavaScript developers
      Backend engineers
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Generate a PDF report with custom text, embedded images, and custom fonts from a Node.js script.

USE CASE 2

Fill out an existing PDF form programmatically, set text fields, checkboxes, and radio buttons, then flatten to lock values.

USE CASE 3

Copy pages from multiple PDF files and merge them into a single combined document.

USE CASE 4

Add a watermark or header text to every page of an existing PDF without re-creating it from scratch.

Tech stack

TypeScript

Getting it running

Difficulty · easy Time to first run · 30min
Free to use for any purpose, personal or commercial, as long as you keep the copyright notice.

In plain English

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.

Copy-paste prompts

Prompt 1
Write Node.js code using pdf-lib to create a one-page PDF with a title, paragraph of body text in a custom font, and an embedded JPEG image.
Prompt 2
Use pdf-lib to open an existing PDF form, fill in all the text fields and checkboxes programmatically, then flatten it so the values are locked.
Prompt 3
Show me how to copy selected pages from two different PDF files and combine them into one output PDF using pdf-lib.
Prompt 4
Add a diagonal watermark text to every page of an existing PDF using pdf-lib in a browser environment with async/await.
Open on GitHub → Explain another repo

← hopding on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.