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.

Every PDF you generate with pdfmake starts from a single plain JavaScript object — the document definition (docDefinition). You pass it to pdfmake.createPdf(docDefinition) and pdfmake handles the rest: layout, pagination, fonts, and rendering. Understanding the shape of this object is the foundation of everything else in pdfmake.

What is the document definition?

The document definition is a declarative description of your entire PDF. It tells pdfmake what content to render, how pages should be sized and oriented, what styles to apply, whether the file should be password-protected, and much more. There is no imperative drawing API — you describe the document as data, and pdfmake figures out the layout.
const pdfDoc = pdfmake.createPdf(docDefinition);
createPdf accepts a plain object and returns a document object you can stream, download, or write to disk. The only field that is strictly required is content.

Top-level structure

Below is a fully-annotated skeleton that shows every major key you can place at the root of a document definition.
const docDefinition = {
  // ── Required ─────────────────────────────────────────────────────────────
  content: [ /* array of content nodes */ ],

  // ── Page layout ──────────────────────────────────────────────────────────
  pageSize: 'A4',                   // string name or { width, height }
  pageOrientation: 'portrait',      // 'portrait' | 'landscape'
  pageMargins: [40, 60, 40, 60],    // number | [h,v] | [left,top,right,bottom]

  // ── Repeating page elements ───────────────────────────────────────────────
  header: function (currentPage, pageCount, pageSize) {
    return { text: `Page ${currentPage} of ${pageCount}`, alignment: 'right' };
  },
  footer: function (currentPage, pageCount) {
    return { text: currentPage.toString(), alignment: 'center' };
  },
  background: function (currentPage, pageSize) {
    return { text: 'DRAFT', opacity: 0.2, fontSize: 60 };
  },

  // ── Watermark ─────────────────────────────────────────────────────────────
  watermark: { text: 'CONFIDENTIAL', color: 'blue', opacity: 0.3, bold: true },

  // ── Styles ────────────────────────────────────────────────────────────────
  styles: {
    header: { fontSize: 18, bold: true },
    subheader: { fontSize: 15, bold: true },
    quote: { italics: true },
    small: { fontSize: 8 }
  },
  defaultStyle: {
    font: 'Roboto',
    fontSize: 12,
    lineHeight: 1.2
  },

  // ── Document metadata ─────────────────────────────────────────────────────
  info: {
    title: 'My Document',
    author: 'Jane Smith',
    subject: 'Annual Report',
    keywords: 'report finance 2024',
    creator: 'pdfmake',
    producer: 'pdfmake'
  },

  // ── Security ──────────────────────────────────────────────────────────────
  userPassword: 'open123',
  ownerPassword: 'owner456',
  permissions: {
    printing: 'highResolution',
    modifying: false,
    copying: false,
    annotating: true,
    fillingForms: true,
    contentAccessibility: true,
    documentAssembly: true
  },

  // ── Compression ───────────────────────────────────────────────────────────
  compress: true,    // default: true

  // ── Dynamic page breaks ───────────────────────────────────────────────────
  pageBreakBefore: function (currentNode, { getFollowingNodesOnPage, getNodesOnNextPage, getPreviousNodesOnPage }) {
    return currentNode.headlineLevel === 1 && getFollowingNodesOnPage().length === 0;
  }
};
Only content is required. Every other key is optional and falls back to a sensible default when omitted.

Key-by-key reference

content

An array of content nodes — text, images, tables, lists, and more. This is the only required field. See the Content Nodes page for the full node type reference.

pageSize / pageOrientation / pageMargins

Control the physical dimensions of every page. Supports standard paper names like 'A4' or custom { width, height } objects. See Page Layout for all options.

header / footer

Functions called for every page that return a content node (or null). Receive currentPage, pageCount, and pageSize as arguments.

background

A function returning a content node rendered behind the page content on every page. Receives currentPage and pageSize. Useful for watermark-style images or tinted backgrounds.

watermark

A shortcut for diagonal text watermarks. Accepts a plain string or an object with text, color, opacity, bold, italics, and fontSize.

styles / defaultStyle

Named style definitions and the document-wide default. Styles cascade through the content tree. See the Styles system section below.

info

PDF metadata embedded in the file: title, author, subject, keywords, creator, and producer.

userPassword / ownerPassword / permissions

Encrypt the PDF and control what readers and owners are allowed to do (print, copy, annotate, etc.).

compress

Boolean (default true). Enables zlib compression of the PDF content stream, reducing file size.

pageBreakBefore

A callback invoked before each node is placed. Return true to force a page break before that node. The second argument is an object with lazy getter functions for node context. See Page Layout for the full signature.

Styles system

pdfmake’s style system lets you define reusable style objects in the top-level styles map, then reference them by name from any content node. Styles can cascade, inherit, and be combined.

Named styles

Define styles in the styles map at the top level of the document definition:
const docDefinition = {
  content: [
    { text: 'This is a header, using header style', style: 'header' },
    'Lorem ipsum dolor sit amet...',
    { text: 'Subheader 1 - using subheader style', style: 'subheader' },
    {
      text: 'This paragraph uses two styles: quote and small.',
      style: ['quote', 'small']  // multiple styles — evaluated left to right
    }
  ],
  styles: {
    header: {
      fontSize: 18,
      bold: true
    },
    subheader: {
      fontSize: 15,
      bold: true
    },
    quote: {
      italics: true
    },
    small: {
      fontSize: 8
    }
  }
};
When you provide an array of style names, they are applied in order. If two styles define the same property, the later style in the array wins.

Style inheritance with extends

A style definition can extend one or more named styles using the extends property. Pass a single style name or an array of style names:
styles: {
  header: {
    fontSize: 18,
    bold: true
  },
  subheader: {
    fontSize: 15,
    extends: 'header'   // inherits bold: true from header
  },
  specialSubheader: {
    fontSize: 13,
    extends: ['header', 'subheader']  // extends can also be an array
  }
}

defaultStyle

The defaultStyle key sets document-wide defaults for every node. Any property set here acts as the baseline before named styles or node-level overrides are applied:
const docDefinition = {
  content: [ /* ... */ ],
  defaultStyle: {
    font: 'Roboto',
    fontSize: 12,
    columnGap: 20
  }
};
defaultStyle is the right place to set the global font family, base font size, or default column gap. You rarely need to repeat these properties on individual nodes.

Inline style overrides

Any styling property (fontSize, bold, color, alignment, etc.) can be set directly on a content node without using a named style:
{
  text: 'This paragraph sets fontSize directly, without a named style',
  fontSize: 25
}
Inline properties override named styles, which override defaultStyle.

Cascade order (lowest → highest priority)

  1. defaultStyle
  2. Named styles (in array order if multiple)
  3. Inline properties on the node itself
Margins behave differently from other properties — they are not inherited by child nodes. A margin set on a stack applies to the stack as a whole, not to the text nodes inside it.

Minimal working example

The simplest possible document definition requires only content:
const docDefinition = {
  content: [
    'First paragraph',
    'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines'
  ]
};

const pdf = pdfmake.createPdf(docDefinition);
pdf.write('output.pdf');

Next steps

Content Nodes

Learn how to build the content array with text, images, tables, lists, columns, and more.

Page Layout

Configure page size, orientation, margins, headers, footers, and multi-section documents.

Build docs developers (and LLMs) love