# @pdfme/pdf-lib
This version is forked from [Hopding/pdf-lib](https://github.com/Hopding/pdf-lib).
Incorporated several bug fixes and additional features into the original code.
2025/03/09 - Add `radius` option for drawRectangle. This enables to render rounded corner rectangles
Draw rounded corners with the specified radius using the Bezier curve function. The radius must be a positive number. If it exceeds half of the width or height, it will be clipped.
2023/12/22 - Incorporate @cantoo/pdf-lib Refactor drawSvg PR
PR: [Refactor drawSvg to handle transfomations as matrix and to crop elements #42](https://github.com/cantoo-scribe/pdf-lib/pull/42)
2023/12/20 - Incorporate @cantoo/pdf-lib that is fork of pdf-lib to use drawSvg method
To achieve [Add SVG Schema #29](https://github.com/pdfme/pdfme/issues/29) feature, pull the [@cantoo/pdf-lib](https://github.com/cantoo-scribe/pdf-lib) into this repo.
2023/04/20 - Character corruption occurs when enabling subsets for some Japanese and Chinese fonts #162
@pdfme/pdf-lib compatible with [foliojs/fontkit](https://github.com/foliojs/fontkit) v2
```js
import { PDFDocument, rgb } from 'pdf-lib'
import fontkit from 'fontkit' // <- Do not use @pdf-lib/fontkit
// This should be a Uint8Array or ArrayBuffer
// This data can be obtained in a number of different ways
// If you're running in a Node environment, you could use fs.readFile()
// In the browser, you could make a fetch() call and use res.arrayBuffer()
const fontBytes = ...
// Create a new PDFDocument
const pdfDoc = await PDFDocument.create()
// Register the `fontkit` instance
pdfDoc.registerFontkit(fontkit)
```
---
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.
> **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