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.

The pdfmake library exposes a singleton instance that serves as the central entry point for all PDF generation. You register fonts and custom table layouts on this instance, configure security policies, then call createPdf() to receive an OutputDocument ready for export. This page documents every method available on the singleton.

Getting the Instance

In Node.js environments, require the compiled entry point. The module exports a pre-constructed singleton — no new call is needed.
const pdfmake = require('pdfmake/js/index');

const doc = pdfmake.createPdf({ content: 'Hello world' });

createPdf()

createPdf is the primary method of the pdfmake instance. It validates the document definition, configures an internal printer with the registered fonts and layouts, and returns an OutputDocument (browser) or OutputDocumentServer (Node.js) that you use to export the PDF.
pdfmake.createPdf(docDefinition, options)
docDefinition
object
required
The document definition object. Must contain at minimum a content property. Supports pageSize, pageMargins, styles, defaultStyle, header, footer, background, images, and more.
const docDefinition = {
  pageSize: 'A4',
  pageMargins: [40, 60, 40, 60],
  content: [
    { text: 'Invoice #1042', style: 'header' },
    { text: 'Thank you for your purchase.' }
  ],
  styles: {
    header: { fontSize: 22, bold: true }
  }
};
options
object
Optional generation options object. Any progressCallback and tableLayouts values you set here will be overwritten by the values registered on the singleton instance via setProgressCallback() and addTableLayouts().
ReturnsOutputDocument in browser environments, OutputDocumentServer in Node.js. Both share a common base; see the Output Document API for the full method list.
On Node.js, if you have not called setUrlAccessPolicy() or setLocalAccessPolicy() before invoking createPdf(), pdfmake will print a warning to the console encouraging you to restrict external resource access.
const doc = pdfmake.createPdf({
  content: [
    { text: 'Report', style: 'title' },
    { text: new Date().toDateString() }
  ],
  styles: {
    title: { fontSize: 28, bold: true, margin: [0, 0, 0, 8] }
  }
});

// Node.js
await doc.write('report.pdf');

// Browser
doc.download('report.pdf');

Font Management

pdfmake requires fonts to be registered on the instance before createPdf() is called. In the browser, Roboto is pre-registered; in Node.js you must provide your own font map. Each font family is an object whose keys are the four style variants: normal, bold, italics, and bolditalics.

addFonts()

Merges additional font definitions into the existing font registry. Existing font families are preserved unless overwritten by the new object.
pdfmake.addFonts(fonts)
fonts
object
required
A font map where each key is a family name and the value is an object with optional normal, bold, italics, and bolditalics string properties pointing to font file paths or VFS keys.
pdfmake.addFonts({
  Inter: {
    normal: 'Inter-Regular.ttf',
    bold: 'Inter-Bold.ttf',
    italics: 'Inter-Italic.ttf',
    bolditalics: 'Inter-BoldItalic.ttf'
  }
});

setFonts()

Replaces the entire fonts registry with the supplied object. Any previously registered fonts — including the browser default Roboto — are discarded.
pdfmake.setFonts(fonts)
fonts
object
required
The complete font map that replaces the current registry.
pdfmake.setFonts({
  Helvetica: {
    normal: 'Helvetica',
    bold: 'Helvetica-Bold',
    italics: 'Helvetica-Oblique',
    bolditalics: 'Helvetica-BoldOblique'
  }
});

clearFonts()

Resets the fonts registry to an empty object. Call this before setFonts() when you want to ensure no previously loaded families linger in the registry.
pdfmake.clearFonts()
pdfmake.clearFonts();
pdfmake.addFonts({ /* only the fonts you need */ });

Table Layout Management

Custom table layouts let you control border widths, padding, and fill colors for individual tables. You register layouts on the instance by name; document definitions then reference them by that name in layout: 'myLayout'.

addTableLayouts()

Merges the supplied layout map into the existing custom layouts registry.
pdfmake.addTableLayouts(tableLayouts)
tableLayouts
object
required
An object whose keys are layout names and whose values are layout descriptor objects. Each descriptor may define hLineWidth, vLineWidth, hLineColor, vLineColor, paddingLeft, paddingRight, paddingTop, paddingBottom, and fillColor as functions or static values.
pdfmake.addTableLayouts({
  zebra: {
    fillColor: (rowIndex) => (rowIndex % 2 === 0 ? '#f3f4f6' : null),
    hLineWidth: () => 0,
    vLineWidth: () => 0,
    paddingLeft: () => 8,
    paddingRight: () => 8
  }
});

setTableLayouts()

Replaces the entire table layouts registry with the supplied object.
pdfmake.setTableLayouts(tableLayouts)
tableLayouts
object
required
The complete table layout map that replaces the current registry.
pdfmake.setTableLayouts({
  noBorders: {
    hLineWidth: () => 0,
    vLineWidth: () => 0
  }
});

clearTableLayouts()

Removes all custom table layouts, resetting the registry to an empty object.
pdfmake.clearTableLayouts()
pdfmake.clearTableLayouts();

Callbacks and Policies

setProgressCallback()

Registers a function that pdfmake calls periodically during document generation to report rendering progress. This is useful for displaying progress bars in long-running documents.
pdfmake.setProgressCallback(callback)
callback
function
required
A function called with a progress value as document pages are processed.
pdfmake.setProgressCallback((progress) => {
  console.log(`Rendering: ${Math.round(progress * 100)}%`);
});

const doc = pdfmake.createPdf({ content: Array(500).fill('Page content.') });
await doc.getBuffer();

setUrlAccessPolicy()

Registers a callback that is invoked for every external URL pdfmake attempts to load (for example, remote images). Return true to allow the request or false to block it.
pdfmake.setUrlAccessPolicy(callback)
callback
function | undefined
required
A function with signature (url: string) => boolean. Pass undefined to remove a previously set policy. On Node.js, omitting this policy emits a console warning when createPdf() is called.
On Node.js, no URL access policy means any URL embedded in a document definition can trigger an outbound HTTP request. Always call setUrlAccessPolicy() in server-side code.
const ALLOWED_ORIGINS = ['https://cdn.example.com'];

pdfmake.setUrlAccessPolicy((url) => {
  try {
    const { origin } = new URL(url);
    return ALLOWED_ORIGINS.includes(origin);
  } catch {
    return false;
  }
});

setLocalAccessPolicy()

Registers a callback that is invoked for every local file path pdfmake attempts to read from disk. Return true to permit access or false to deny it.
pdfmake.setLocalAccessPolicy(callback)
setLocalAccessPolicy() is available only in the Node.js build (src/index.js). It is not present in the browser bundle.
callback
function | undefined
required
A function with signature (path: string) => boolean. Pass undefined to remove a previously set policy. On Node.js, omitting this policy emits a console warning when createPdf() is called.
const ASSETS_DIR = '/var/app/assets';

pdfmake.setLocalAccessPolicy((filePath) => {
  return filePath.startsWith(ASSETS_DIR);
});

Browser-Only Methods

The following methods are available only on the browser build of pdfmake (build/pdfmake.js). They manage the in-memory virtual file system that the browser uses in place of the Node.js fs module.

addVirtualFileSystem()

Iterates over the supplied VFS map and writes each entry into the virtual file system. Each entry can be a raw base64 string or an object with data and optional encoding fields (defaults to 'base64').
pdfmake.addVirtualFileSystem(vfs)
vfs
object
required
A map from file name (string key) to either a base64-encoded string or an object { data: string, encoding?: string }. This is the same shape produced by the pdfmake build-vfs.js script.
import pdfmake from 'pdfmake/build/pdfmake';
import pdfFonts from 'pdfmake/build/vfs_fonts';

// Register the bundled Roboto VFS
pdfmake.addVirtualFileSystem(pdfFonts);

addFontContainer()

Convenience method that registers a font container — an object with both a vfs map and a fonts map — in a single call. It is equivalent to calling addVirtualFileSystem(fontContainer.vfs) followed by addFonts(fontContainer.fonts).
pdfmake.addFontContainer(fontContainer)
fontContainer
object
required
An object with two required properties:
  • vfs — a VFS map (same shape as addVirtualFileSystem)
  • fonts — a font registry map (same shape as addFonts)
import pdfmake from 'pdfmake/build/pdfmake';

// A font package that ships both vfs and font definitions together
import interFontContainer from './fonts/inter-container';

pdfmake.addFontContainer(interFontContainer);

const doc = pdfmake.createPdf({
  defaultStyle: { font: 'Inter' },
  content: 'Hello in Inter!'
});
doc.download('hello.pdf');

Build docs developers (and LLMs) love