Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 16 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,20 @@

> **Learn more at [pdf-lib.js.org](https://pdf-lib.js.org)**

## Fork Notice

This repository is a fork of [`Hopding/pdf-lib`](https://github.com/Hopding/pdf-lib).

We forked it to add HarfBuzz-based font shaping and subsetting so `pdf-lib` can support more languages and more complex scripts, while removing the previous Fontkit dependency for custom font handling.

In this fork:

- HarfBuzz is used for custom font embedding and shaping.
- Fontkit is no longer required for custom font support.

## Table of Contents

- [Fork Notice](#fork-notice)
- [Features](#features)
- [Motivation](#motivation)
- [Usage Examples](#usage-examples)
Expand Down Expand Up @@ -642,9 +654,7 @@ const pdfBytes = await pdfDoc.save()

### Embed Font and Measure Text

`pdf-lib` relies on a sister module to support embedding custom fonts: [`@pdf-lib/fontkit`](https://www.npmjs.com/package/@pdf-lib/fontkit). You must add the `@pdf-lib/fontkit` module to your project and register it using `pdfDoc.registerFontkit(...)` before embedding custom fonts.

> **[See below for detailed installation instructions on installing `@pdf-lib/fontkit` as a UMD or NPM module.](#fontkit-installation)**
`pdf-lib` can embed and subset custom OpenType/TrueType fonts directly. No extra font engine needs to be installed or registered before calling `pdfDoc.embedFont(...)`.

_This example produces [this PDF](assets/pdfs/examples/embed_font_and_measure_text.pdf)_ (when [this font](assets/fonts/ubuntu/Ubuntu-R.ttf) is used for the `fontBytes` variable).

Expand All @@ -653,7 +663,6 @@ _This example produces [this PDF](assets/pdfs/examples/embed_font_and_measure_te
<!-- prettier-ignore -->
```js
import { PDFDocument, rgb } from 'pdf-lib'
import fontkit from '@pdf-lib/fontkit'

// This should be a Uint8Array or ArrayBuffer
// This data can be obtained in a number of different ways
Expand All @@ -664,9 +673,6 @@ const fontBytes = ...
// Create a new PDFDocument
const pdfDoc = await PDFDocument.create()

// Register the `fontkit` instance
pdfDoc.registerFontkit(fontkit)

// Embed our custom font in the document
const customFont = await pdfDoc.embedFont(fontBytes)

Expand Down Expand Up @@ -1006,7 +1012,7 @@ const pdfBytes = await pdfDoc.save()

## Deno Usage

`pdf-lib` fully supports the exciting new [Deno](https://deno.land/) runtime! All of the [usage examples](#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](https://www.skypack.dev/) CDN, because Deno requires all modules to be referenced via URLs.
`pdf-lib` fully supports the exciting new [Deno](https://deno.land/) runtime! All of the [usage examples](#usage-examples) work in Deno. The only thing you need to do is change the import for `pdf-lib` to use the [Skypack](https://www.skypack.dev/) 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](https://medium.com/swlh/how-to-create-and-modify-pdf-files-in-deno-ffaad7099b0?source=friends_link&sk=3da183bb776d059df428eaea52102f19)**

Expand Down Expand Up @@ -1059,14 +1065,11 @@ import {
rgb,
StandardFonts,
} from 'https://cdn.skypack.dev/pdf-lib@^1.11.1?dts';
import fontkit from 'https://cdn.skypack.dev/@pdf-lib/fontkit@^1.0.0?dts';

const url = 'https://pdf-lib.js.org/assets/ubuntu/Ubuntu-R.ttf';
const fontBytes = await fetch(url).then((res) => res.arrayBuffer());

const pdfDoc = await PDFDocument.create();

pdfDoc.registerFontkit(fontkit);
const customFont = await pdfDoc.embedFont(fontBytes);

const page = pdfDoc.addPage();
Expand Down Expand Up @@ -1159,54 +1162,9 @@ var PDFDocument = PDFLib.PDFDocument;
var rgb = PDFLib.rgb;
```

## Fontkit Installation

`pdf-lib` relies upon a sister module to support embedding custom fonts: [`@pdf-lib/fontkit`](https://www.npmjs.com/package/@pdf-lib/fontkit). You must add the `@pdf-lib/fontkit` module to your project and register it using `pdfDoc.registerFontkit(...)` before embedding custom fonts (see the [font embedding example](#embed-font-and-measure-text)). This module is not included by default because not all users need it, and it increases bundle size.

Installing this module is easy. Just like `pdf-lib` itself, `@pdf-lib/fontkit` can be installed with `npm`/`yarn` or as a UMD module.

### Fontkit NPM Module

```bash
# With npm
npm install --save @pdf-lib/fontkit

# With yarn
yarn add @pdf-lib/fontkit
```

To register the `fontkit` instance:
## Custom Fonts

<!-- prettier-ignore -->
```js
import { PDFDocument } from 'pdf-lib'
import fontkit from '@pdf-lib/fontkit'

const pdfDoc = await PDFDocument.create()
pdfDoc.registerFontkit(fontkit)
```

### Fontkit UMD Module

The following builds are available:

- https://unpkg.com/@pdf-lib/fontkit/dist/fontkit.umd.js
- https://unpkg.com/@pdf-lib/fontkit/dist/fontkit.umd.min.js
- https://cdn.jsdelivr.net/npm/@pdf-lib/fontkit/dist/fontkit.umd.js
- https://cdn.jsdelivr.net/npm/@pdf-lib/fontkit/dist/fontkit.umd.min.js

> **NOTE:** if you are using the CDN scripts in production, you should include a specific version number in the URL, for example:
>
> - https://unpkg.com/@pdf-lib/fontkit@0.0.4/dist/fontkit.umd.min.js
> - https://cdn.jsdelivr.net/npm/@pdf-lib/fontkit@0.0.4/dist/fontkit.umd.min.js

When using a UMD build, you will have access to a global `window.fontkit` variable. To register the `fontkit` instance:

<!-- prettier-ignore -->
```js
var pdfDoc = await PDFLib.PDFDocument.create()
pdfDoc.registerFontkit(fontkit)
```
Embedding custom fonts no longer requires an external font engine. Load the font bytes and pass them directly to `pdfDoc.embedFont(...)`.

## Documentation

Expand Down Expand Up @@ -1237,14 +1195,12 @@ When working with PDFs, you will frequently come across the terms "character enc
<!-- prettier-ignore -->
```js
import { PDFDocument } from 'pdf-lib'
import fontkit from '@pdf-lib/fontkit'

const url = 'https://pdf-lib.js.org/assets/ubuntu/Ubuntu-R.ttf'
const fontBytes = await fetch(url).then((res) => res.arrayBuffer())

const pdfDoc = await PDFDocument.create()

pdfDoc.registerFontkit(fontkit)
const ubuntuFont = await pdfDoc.embedFont(fontBytes)

const page = pdfDoc.addPage()
Expand Down Expand Up @@ -1289,7 +1245,6 @@ You can use an embedded font when filling form fields as follows:

```js
import { PDFDocument } from 'pdf-lib';
import fontkit from '@pdf-lib/fontkit';

// Fetch the PDF with form fields
const formUrl = 'https://pdf-lib.js.org/assets/dod_character.pdf';
Expand All @@ -1303,7 +1258,6 @@ const fontBytes = await fetch(fontUrl).then((res) => res.arrayBuffer());
const pdfDoc = await PDFDocument.load(formBytes);

// Embed the Ubuntu font
pdfDoc.registerFontkit(fontkit);
const ubuntuFont = await pdfDoc.embedFont(fontBytes);

// Get two text fields from the form
Expand Down
2 changes: 0 additions & 2 deletions apps/deno/tests/test1.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fontkit from 'https://cdn.skypack.dev/@pdf-lib/fontkit@^1.0.0?dts';
import { Assets } from '../index.ts';

// @deno-types="../dummy.d.ts"
Expand Down Expand Up @@ -45,7 +44,6 @@ export default async (assets: Assets) => {
pdfDoc.setCreationDate(new Date('2018-06-24T01:58:37.228Z'));
pdfDoc.setModificationDate(new Date('2018-12-21T07:00:11.000Z'));

pdfDoc.registerFontkit(fontkit);

await pdfDoc.attach(assets.images.png.greyscale_bird, 'bird.png', {
mimeType: 'image/png',
Expand Down
2 changes: 0 additions & 2 deletions apps/deno/tests/test11.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fontkit from 'https://cdn.skypack.dev/@pdf-lib/fontkit@^1.0.0?dts';
import { Assets } from '../index.ts';

// @deno-types="../dummy.d.ts"
Expand Down Expand Up @@ -60,7 +59,6 @@ export default async (assets: Assets) => {

const pdfDoc = await PDFDocument.create();

pdfDoc.registerFontkit(fontkit);

const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);
const helveticaBoldFont = await pdfDoc.embedFont(StandardFonts.HelveticaBold);
Expand Down
2 changes: 0 additions & 2 deletions apps/deno/tests/test17.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fontkit from 'https://cdn.skypack.dev/@pdf-lib/fontkit@^1.0.0?dts';

import { Assets } from '../index.ts';

Expand All @@ -18,7 +17,6 @@ import {
export default async (assets: Assets) => {
const pdfDoc = await PDFDocument.load(assets.pdfs.fancy_fields);

pdfDoc.registerFontkit(fontkit);
const ubuntuFont = await pdfDoc.embedFont(assets.fonts.ttf.ubuntu_r);

const form = pdfDoc.getForm();
Expand Down
2 changes: 0 additions & 2 deletions apps/deno/tests/test18.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fontkit from 'https://cdn.skypack.dev/@pdf-lib/fontkit@^1.0.0?dts';

import { Assets } from '../index.ts';

Expand Down Expand Up @@ -130,7 +129,6 @@ const loadC = async (assets: Assets) => {
const loadD = async (assets: Assets) => {
const pdfDoc = await PDFDocument.load(assets.pdfs.fancy_fields);

pdfDoc.registerFontkit(fontkit);
const ubuntuFont = await pdfDoc.embedFont(assets.fonts.ttf.ubuntu_r);

const form = pdfDoc.getForm();
Expand Down
2 changes: 0 additions & 2 deletions apps/deno/tests/test2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fontkit from 'https://cdn.skypack.dev/@pdf-lib/fontkit@^1.0.0?dts';
import { Assets } from '../index.ts';

// @deno-types="../dummy.d.ts"
Expand All @@ -20,7 +19,6 @@ export default async (assets: Assets) => {
updateMetadata: false,
});

pdfDoc.registerFontkit(fontkit);

const ubuntuFont = await pdfDoc.embedFont(fonts.ttf.ubuntu_r, {
subset: true,
Expand Down
2 changes: 0 additions & 2 deletions apps/deno/tests/test6.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fontkit from 'https://cdn.skypack.dev/@pdf-lib/fontkit@^1.0.0?dts';
import { Assets } from '../index.ts';

// @deno-types="../dummy.d.ts"
Expand All @@ -17,7 +16,6 @@ export default async (assets: Assets) => {
{ parseSpeed: ParseSpeeds.Fastest },
);

pdfDoc.registerFontkit(fontkit);

await pdfDoc.attach(pdfs.us_constitution, 'us_constitution.pdf', {
mimeType: 'application/pdf',
Expand Down
2 changes: 0 additions & 2 deletions apps/deno/tests/test9.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fontkit from 'https://cdn.skypack.dev/@pdf-lib/fontkit@^1.0.0?dts';
import { Assets } from '../index.ts';

// @deno-types="../dummy.d.ts"
Expand All @@ -16,7 +15,6 @@ export default async (assets: Assets) => {
parseSpeed: ParseSpeeds.Fastest,
});

pdfDoc.registerFontkit(fontkit);

const ubuntuFont = await pdfDoc.embedFont(fonts.ttf.ubuntu_r, {
subset: true,
Expand Down
2 changes: 0 additions & 2 deletions apps/node/tests/test1.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fontkit from '@pdf-lib/fontkit';
import { Assets } from '..';
import {
clip,
Expand Down Expand Up @@ -43,7 +42,6 @@ export default async (assets: Assets) => {
pdfDoc.setCreationDate(new Date('2018-06-24T01:58:37.228Z'));
pdfDoc.setModificationDate(new Date('2018-12-21T07:00:11.000Z'));

pdfDoc.registerFontkit(fontkit);

await pdfDoc.attach(assets.images.png.greyscale_bird, 'bird.png', {
mimeType: 'image/png',
Expand Down
2 changes: 0 additions & 2 deletions apps/node/tests/test11.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fontkit from '@pdf-lib/fontkit';
import { Assets } from '..';
import {
last,
Expand Down Expand Up @@ -58,7 +57,6 @@ export default async (assets: Assets) => {

const pdfDoc = await PDFDocument.create();

pdfDoc.registerFontkit(fontkit);

const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);
const helveticaBoldFont = await pdfDoc.embedFont(StandardFonts.HelveticaBold);
Expand Down
2 changes: 0 additions & 2 deletions apps/node/tests/test17.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ import {
PDFWidgetAnnotation,
} from '../../..';

import fontkit from '@pdf-lib/fontkit';

export default async (assets: Assets) => {
const pdfDoc = await PDFDocument.load(assets.pdfs.fancy_fields);

pdfDoc.registerFontkit(fontkit);
const ubuntuFont = await pdfDoc.embedFont(assets.fonts.ttf.ubuntu_r);

const form = pdfDoc.getForm();
Expand Down
2 changes: 0 additions & 2 deletions apps/node/tests/test18.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Assets } from '..';
import { PDFDocument } from '../../..';

import fontkit from '@pdf-lib/fontkit';

// Based on test14.ts
const loadA = async (assets: Assets) => {
Expand Down Expand Up @@ -128,7 +127,6 @@ const loadC = async (assets: Assets) => {
const loadD = async (assets: Assets) => {
const pdfDoc = await PDFDocument.load(assets.pdfs.fancy_fields);

pdfDoc.registerFontkit(fontkit);
const ubuntuFont = await pdfDoc.embedFont(assets.fonts.ttf.ubuntu_r);

const form = pdfDoc.getForm();
Expand Down
2 changes: 0 additions & 2 deletions apps/node/tests/test2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fontkit from '@pdf-lib/fontkit';
import { Assets } from '..';
import { ParseSpeeds, PDFDocument, rgb } from '../../..';

Expand All @@ -13,7 +12,6 @@ export default async (assets: Assets) => {
updateMetadata: false,
});

pdfDoc.registerFontkit(fontkit);

const ubuntuFont = await pdfDoc.embedFont(fonts.ttf.ubuntu_r, {
subset: true,
Expand Down
2 changes: 0 additions & 2 deletions apps/node/tests/test6.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fontkit from '@pdf-lib/fontkit';
import { Assets } from '..';
import { degrees, ParseSpeeds, PDFDocument, rgb } from '../../..';

Expand All @@ -10,7 +9,6 @@ export default async (assets: Assets) => {
{ parseSpeed: ParseSpeeds.Fastest },
);

pdfDoc.registerFontkit(fontkit);

await pdfDoc.attach(pdfs.us_constitution, 'us_constitution.pdf', {
mimeType: 'application/pdf',
Expand Down
2 changes: 0 additions & 2 deletions apps/node/tests/test9.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fontkit from '@pdf-lib/fontkit';
import { Assets } from '..';
import { ParseSpeeds, PDFDocument, rgb } from '../../..';

Expand All @@ -9,7 +8,6 @@ export default async (assets: Assets) => {
parseSpeed: ParseSpeeds.Fastest,
});

pdfDoc.registerFontkit(fontkit);

const ubuntuFont = await pdfDoc.embedFont(fonts.ttf.ubuntu_r, {
subset: true,
Expand Down
1 change: 0 additions & 1 deletion apps/rn/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"test": "jest"
},
"dependencies": {
"@pdf-lib/fontkit": "^0.0.4",
"pdf-lib": "./../..",
"react": "16.8.3",
"react-native": "0.59.10",
Expand Down
2 changes: 0 additions & 2 deletions apps/rn/src/tests/test1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fontkit from '@pdf-lib/fontkit';
import {
clip,
clipEvenOdd,
Expand Down Expand Up @@ -44,7 +43,6 @@ export default async () => {
pdfDoc.setCreationDate(new Date('2018-06-24T01:58:37.228Z'));
pdfDoc.setModificationDate(new Date('2018-12-21T07:00:11.000Z'));

pdfDoc.registerFontkit(fontkit);

await pdfDoc.attach(
await fetchAsset('images/greyscale_bird.png'),
Expand Down
2 changes: 0 additions & 2 deletions apps/rn/src/tests/test11.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fontkit from '@pdf-lib/fontkit';
import { PDFDocument, StandardFonts, last, charAtIndex } from 'pdf-lib';

import { fetchAsset, writePdf } from './assets';
Expand Down Expand Up @@ -45,7 +44,6 @@ export default async () => {

const pdfDoc = await PDFDocument.create();

pdfDoc.registerFontkit(fontkit);

const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);
const helveticaBoldFont = await pdfDoc.embedFont(StandardFonts.HelveticaBold);
Expand Down
2 changes: 0 additions & 2 deletions apps/rn/src/tests/test17.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
drawEllipse,
PDFWidgetAnnotation,
} from 'pdf-lib';
import fontkit from '@pdf-lib/fontkit';

import { fetchAsset, writePdf } from './assets';

Expand All @@ -21,7 +20,6 @@ export default async () => {

const pdfDoc = await PDFDocument.load(fancyFieldsPdf);

pdfDoc.registerFontkit(fontkit);
const ubuntuFont = await pdfDoc.embedFont(ubuntuR);

const form = pdfDoc.getForm();
Expand Down
Loading
Loading