🌐
GitHub
github.com › Hopding › pdf-lib
GitHub - Hopding/pdf-lib: Create and modify PDF documents in any JavaScript environment
pdf-lib relies upon a sister module to support embedding custom fonts: @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 ...
Starred by 8.1K users
Forked by 828 users
Languages   TypeScript 80.9% | HTML 9.9% | JavaScript 8.5% | Objective-C 0.3% | CSS 0.2% | Starlark 0.1% | Java 0.1%
🌐
npm
npmjs.com › package › @pdf-lib › fontkit
pdf-lib/fontkit
npm i @pdf-lib/fontkit · github.com/Hopding/fontkit · github.com/Hopding/fontkit · 213,930 · 1.1.1 · MIT · 4.3 MB · 25 · 5 years ago · Try on RunKit ·
      » npm install @pdf-lib/fontkit
    
Published   Nov 28, 2020
Version   1.1.1
Author   Andrew Dillon
🌐
GitHub
github.com › Hopding › pdf-lib › discussions › 1480
Loading font using fontkit · Hopding/pdf-lib · Discussion #1480
import { PDFDocument } from 'pdf-lib'; import fontKit from '@pdf-lib/fontkit'; import Noto from 'url:./NotoSansJP-Regular.ttf'; async function main() { const test = await fetch(Noto).then((res) => res.arrayBuffer()); const pdfDoc = await PDFDocument.create(); pdfDoc.registerFontkit(fontKit); const customFont = await pdfDoc.embedFont(test); } main();
Author   Hopding
🌐
GitHub
github.com › Hopding › pdf-lib › blob › master › src › types › fontkit.ts
pdf-lib/src/types/fontkit.ts at master · Hopding/pdf-lib
Create and modify PDF documents in any JavaScript environment - pdf-lib/src/types/fontkit.ts at master · Hopding/pdf-lib
Author   Hopding
🌐
UNPKG
app.unpkg.com › @pdf-lib › [email protected] › files › README.md
UNPKG
# Purpose of this Fork This project is a fork of https://github.com/foliojs/fontkit created for use in https://github.com/Hopding/pdf-lib.
🌐
GitHub
github.com › ExodusMovement › pdf-lib-fontkit
GitHub - ExodusMovement/pdf-lib-fontkit: An advanced font engine for Node and the browser
An advanced font engine for Node and the browser. Contribute to ExodusMovement/pdf-lib-fontkit development by creating an account on GitHub.
Author   ExodusMovement
🌐
UNPKG
app.unpkg.com › @pdf-lib › [email protected] › files › README.md
pdf-lib/fontkit
# Purpose of this Fork This project is a fork of https://github.com/foliojs/fontkit created for use in https://github.com/Hopding/pdf-lib.
🌐
GitHub
github.com › Hopding › pdf-lib › issues › 195
Cannot import `fontkit` from @pdf-lib/fontkit · Issue #195 · Hopding/pdf-lib
September 22, 2019 - I am building an Angular project and trying to import fontkit as described in the README instruction. import fontkit from '@pdf-lib/fontkit' gives an error that says Module '"/node_modules/@pdf-lib/fontkit/fontkit"' has no default export...
Published   Sep 22, 2019
🌐
npm
npmjs.com › package › @btielen › pdf-lib-fontkit
@btielen/pdf-lib-fontkit - npm
npm i @btielen/pdf-lib-fontkit · github.com/btielen/fontkit · github.com/btielen/fontkit · 326 · 1.0.0 · MIT · 1.47 MB · 3 · 4 years ago · bennot · Try on RunKit ·
      » npm install @btielen/pdf-lib-fontkit
    
Published   Feb 04, 2022
Version   1.0.0
Author   Andrew Dillon
Top answer
1 of 3
22

From the specs https://www.npmjs.com/package/pdf-lib#embed-font-and-measure-text

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

We have to npm i --save @pdf-lib/fontkit and we have to have source from where which we will read the font. In my case I have added .otf file in project and loaded font. Files are structured like on the image:

import path from 'path';
import fs from 'fs';
import {PDFDocument, PDFForm, StandardFonts, PDFFont} from 'pdf-lib';
import fontkit from '@pdf-lib/fontkit';

const pdfBytes = fs.readFileSync(path.join(__dirname, `/w_template/` + fileName + '.pdf'));
const pdfDoc = await PDFDocument.load(pdfBytes);

pdfDoc.registerFontkit(fontkit);
//load font and embed it to pdf document
const fontBytes = fs.readFileSync(path.join(__dirname, 'HouschkaHead-BoldItalic.otf'));
const customFont = await pdfDoc.embedFont(fontBytes);

const form = pdfDoc.getForm();
const textField = form.getTextField('signature');
textField.setFontSize(11);
textField.setText('stefan z');
textField.updateAppearances(customFont);

// form flatten is available from v1.16.0 and it makes form read-only (not editable)
form.flatten();
const modifiedPdf = await pdfDoc.save();

And this is the final result: check how the signature input form field is different from rest of input fields which are filled with default font Bonus: if you want to play with color of the text of inputs in form, this is what I have found while digging more under the library source code (it might be not optimal, but it can give you starting point for more things):

import {setFillingRgbColor} from 'pdf-lib'

const textField = form.getTextField(fieldName);
const da = textField.acroField.getDefaultAppearance() ?? '';
const newDa = da + '\n' + setFillingRgbColor(1, 0, 0).toString(); 
textField.acroField.setDefaultAppearance(newDa);

2 of 3
7

If anyone is struggling to change the fontSize in filling text fields using the latest inbuilt function called setFontSize(). You can use this approach.

const { PDFDocument, setFontAndSize } = require('pdf-lib');

   const pdfDoc = await PDFDocument.load(file);
   const form = pdfDoc.getForm();

const textField = form.getTextField('106Mobile.1');
    textField.setText('This works fine');
    const da = textField.acroField.getDefaultAppearance() ?? '';
    const newDa = da + '\n' + setFontAndSize('Courier', 8).toString(); //setFontAndSize() method came to resuce
    textField.acroField.setDefaultAppearance(newDa);

I had to use this because I got an error saying

No /DA (default appearance) entry found for field: 106Title.1

when setting font sizes to some text fields (not all of it). But I degenerately had those text fields. Fixed it using the above workaround.

Find elsewhere
🌐
UNPKG
unpkg.com › browse › @pdf-lib › [email protected] › README.md
pdf-lib/fontkit/README.md
# Purpose of this Fork This project is a fork of https://github.com/foliojs/fontkit created for use in https://github.com/Hopding/pdf-lib.
🌐
CodeSandbox
codesandbox.io › examples › package › @pdf-lib › fontkit
pdf-lib/fontkit examples
pdf-lib-example · nabeponFind more examples or templates · AboutAn advanced font engine for Node and the browser274,212Weekly Downloads · Latest version1.1.1 · LicenseMIT · External Links · github.com/Hopding/fontkit · github.com/Hopding/fontkit/issues ·
🌐
GitHub
github.com › Hopding › pdf-lib › issues › 372
Cannot embed custom font from local file · Issue #372 · Hopding/pdf-lib
March 2, 2020 - You must register a fontkit instance with PDFDocument.registerFontkit(...) before embedding custom fonts.
Published   Mar 02, 2020
🌐
jsDelivr
jsdelivr.com › package › npm › @pdf-lib › fontkit
pdf-lib/fontkit - A CDN for npm and GitHub
November 28, 2020 - A free, fast, and reliable CDN for @pdf-lib/fontkit. An advanced font engine for Node and the browser
Published   Dec 19, 2018
🌐
GitHub
github.com › Hopding › pdf-lib › blob › master › README.md
pdf-lib/README.md at master · Hopding/pdf-lib
pdf-lib relies on a sister module to support embedding custom fonts: @pdf-lib/fontkit.
Author   Hopding
🌐
Nodenpm
nodenpm.com › @pdf-lib › fontkit › package.html
@pdf-lib/fontkit 0.0.4 node npm open source project details/download - NodeNpm
NodeJsNpm » Andrew Dillon » @pdf-lib/fontkit » 0.0.4 · An advanced font engine for Node and the browser opentype font typography subset emoji glyph layout · # Purpose of this Fork This project is a fork of https://github.com/foliojs/fontkit created for use in https://github.com/Hopding/pdf-lib.
🌐
GitHub
github.com › Hopding › pdf-lib › issues › 871
Hopding/pdf-lib
May 5, 2021 - const aktivGroteskExThinUrl = "<font-url>/aktivGroteskExThin.ttf"; const url = `some-pdf.pdf` const existingPdfBytes = await fetch(url).then(res => res.arrayBuffer()) const pdfDoc = await PDFDocument.load(existingPdfBytes) // register fontKit to use custom fonts pdfDoc.registerFontkit(fontkit); // define fonts const res = await fetch(aktivGroteskExThinUrl); const customFont = await res.arrayBuffer(); const aktivGroteskExThinFont = await pdfDoc.embedFont(customFont); // define first Page layout const pages = pdfDoc.getPages() const firstPage = pages[0] // top left firstPage.drawText(`Some Text with double ff-char combination: Pfeffer `, { x: 17, y: 193, size: 6, font: aktivGroteskExThinFont })
Published   May 05, 2021
🌐
UNPKG
unpkg.com › browse › @pdf-lib › [email protected] › dist
pdf-lib/fontkit
An advanced font engine for Node and the browser · github.com/Hopding/fontkit
🌐
GitHub
github.com › rivy › js.pdf-lib
GitHub - rivy/js.pdf-lib: Create and modify PDF documents in any JavaScript environment
pdf-lib relies upon a sister module to support embedding custom fonts: @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 ...
Author   rivy
🌐
GitHub
github.com › Hopding › pdf-lib › issues › 1492
Resaving document with fontkit after adding text results in error · Issue #1492 · Hopding/pdf-lib
May 21, 2023 - <html><head> <script src="https://unpkg.com/[email protected]/dist/pdf-lib.js"></script> <script src="https://unpkg.com/@pdf-lib/[email protected]/dist/fontkit.umd.js"></script> <script> (async() => { const link = 'https://fonts.cdnfonts.com/s/29105/ARIAL.woff' //Cannot read properties of undefined (reading 'advanceWidth') //const link = 'https://fonts.gstatic.com/s/roboto/v30/KFOkCnqEu92Fr1MmgVxMIzIFKw.woff2' //index out of range const fontBytes = await fetch(link).then(res => res.arrayBuffer()); const pdfDoc = await PDFLib.PDFDocument.create(); pdfDoc.registerFontkit(window.fontkit); const font = a
Published   Aug 02, 2023