Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/bpampuch/pdfmake/llms.txt

Use this file to discover all available pages before exploring further.

pdfmake embeds fonts directly in the generated PDF so the document looks identical everywhere it is opened. To accomplish this, it uses a Virtual File System (VFS) — an in-memory map of filenames to base64-encoded font data — that lets both browser and Node.js environments load font files in the same way.

The Virtual File System (VFS)

The VFS is a plain JavaScript object whose keys are filenames and whose values are either base64-encoded strings or raw Buffer/Uint8Array data. pdfmake reads font files from the VFS at render time rather than from the real filesystem, which is why the same API works in the browser and on the server. The standard distribution ships a pre-built VFS (pdfmake/build/vfs_fonts.js) that contains the Roboto typeface in four variants: regular, medium (used as bold), italic, and medium-italic.

Default Setup: Roboto in the Browser

In a browser bundling workflow, import the pre-built VFS and register it using addVirtualFileSystem before calling createPdf:
import pdfMake from 'pdfmake/build/pdfmake';
import pdfFonts from 'pdfmake/build/vfs_fonts';

pdfMake.addVirtualFileSystem(pdfFonts);

const docDefinition = { content: ['Hello world'] };
pdfMake.createPdf(docDefinition).open();
When pdfmake/build/vfs_fonts.js is loaded directly as a <script> tag in a browser, it automatically calls pdfMake.addVirtualFileSystem(vfs) on the global pdfMake instance — no additional setup is required. The browser build already registers Roboto as the default font family, so no additional font configuration is needed for documents that use the default font.

Default Setup: Roboto in Node.js

In Node.js the pdfmake package exports a server-side instance. The Roboto font object lives in a separate module under fonts/Roboto:
const pdfmake = require('pdfmake');
const Roboto = require('pdfmake/fonts/Roboto');

pdfmake.addFonts(Roboto);

const docDefinition = { content: ['Hello world'] };
const pdf = pdfmake.createPdf(docDefinition);
pdf.write('output.pdf');
Roboto is a plain object of the shape { Roboto: { normal, bold, italics, bolditalics } } where each value is a file path on disk that pdfmake resolves via the local access policy.

Font Definition Object

Regardless of environment, fonts are registered as an object where each key is the family name used in content/styles, and each value is an object mapping the four style variants to a VFS key or file path:
{
  MyCustomFont: {
    normal:      'MyCustomFont-Regular.ttf',
    bold:        'MyCustomFont-Bold.ttf',
    italics:     'MyCustomFont-Italic.ttf',
    bolditalics: 'MyCustomFont-BoldItalic.ttf'
  }
}
You do not have to provide all four variants. If a variant is missing, pdfmake falls back to normal. The values are looked up as keys in the VFS (browser) or as file paths on disk (Node.js).

Font Management Methods

The pdfmake instance exposes three methods for managing registered fonts:
addFonts(fonts)
method
Merges fonts into the current font registry. Use this to add families without removing the ones already registered (such as Roboto).
setFonts(fonts)
method
Replaces the entire font registry with fonts. All previously registered families (including Roboto) are removed.
clearFonts()
method
Removes all registered fonts. Call before setFonts or addFonts when you need a clean slate.
// Add a second font family alongside Roboto
pdfmake.addFonts({
  Helvetica: {
    normal:      'Helvetica',
    bold:        'Helvetica-Bold',
    italics:     'Helvetica-Oblique',
    bolditalics: 'Helvetica-BoldOblique'
  }
});

// Use it in content
const docDefinition = {
  content: [
    { text: 'This uses the custom font', font: 'Helvetica' },
    { text: 'This still uses Roboto' }
  ]
};

Using a Custom Font in Node.js

1

Prepare your font files

Place your TrueType (.ttf) or OpenType (.otf) files in a directory accessible to your Node.js process, for example ./fonts/.
2

Define the font object

Create an object mapping the family name to the four variant file paths:
const myFonts = {
  OpenSans: {
    normal:      'fonts/OpenSans-Regular.ttf',
    bold:        'fonts/OpenSans-Bold.ttf',
    italics:     'fonts/OpenSans-Italic.ttf',
    bolditalics: 'fonts/OpenSans-BoldItalic.ttf'
  }
};
3

Register the font

const pdfmake = require('pdfmake');
pdfmake.addFonts(myFonts);
4

Use it in your document

const docDefinition = {
  defaultStyle: { font: 'OpenSans' },
  content: [
    { text: 'This document uses Open Sans throughout.' },
    { text: 'This is bold.', bold: true },
    { text: 'This is italic.', italics: true }
  ]
};

pdfmake.createPdf(docDefinition).write('output.pdf');
Node.js resolves font file paths relative to the current working directory of the process (process.cwd()), not relative to the source file. Make sure the paths you provide are correct relative to where you run node.

Browser Methods: VFS and Font Containers

The browser build of pdfmake provides two additional helper methods beyond addFonts/setFonts/clearFonts:

addVirtualFileSystem(vfs)

Merges an object of { filename: base64Data } entries into the in-memory VFS. This is the low-level method the browser build uses to make font data available before rendering.
pdfMake.addVirtualFileSystem({
  'OpenSans-Regular.ttf': '<base64-encoded font data>',
  'OpenSans-Bold.ttf': '<base64-encoded font data>'
});

pdfMake.addFonts({
  OpenSans: {
    normal: 'OpenSans-Regular.ttf',
    bold:   'OpenSans-Bold.ttf'
  }
});

addFontContainer(fontContainer)

A convenience method that calls addVirtualFileSystem(fontContainer.vfs) and addFonts(fontContainer.fonts) in one step. A font container is any object with a vfs key (the VFS entries) and a fonts key (the font definition object):
// fontContainer shape
const myFontContainer = {
  vfs: {
    'OpenSans-Regular.ttf': '<base64>',
    'OpenSans-Bold.ttf':    '<base64>'
  },
  fonts: {
    OpenSans: {
      normal: 'OpenSans-Regular.ttf',
      bold:   'OpenSans-Bold.ttf'
    }
  }
};

pdfMake.addFontContainer(myFontContainer);
The browser font modules (e.g. pdfmake/build/vfs_fonts) export a flat VFS object — a map of filenames to base64 data. Pass it directly to addVirtualFileSystem. The browser build pre-registers the Roboto font definition on startup, so adding the Roboto VFS is enough to use the default font.

Building a Custom VFS Bundle

For browser projects that need custom fonts, the recommended approach is to build a custom vfs_fonts.js file using the build-vfs.js script included in the pdfmake repository:
# Clone the pdfmake repository
git clone https://github.com/bpampuch/pdfmake.git
cd pdfmake

# Place your .ttf files in a directory, then run:
node build-vfs.js path/to/fonts/
The build script encodes every font file in the specified directory as base64 and writes a new build/vfs_fonts.js that you can import into your project in place of the default file.
Only TrueType (.ttf) fonts are supported by pdfmake’s font subsetting engine. OpenType fonts with CFF outlines (.otf) may not render correctly.

Build docs developers (and LLMs) love