pdfmake can automatically build a table of contents by letting you mark headings and other text nodes with aDocumentation 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.
tocItem flag. When the document is rendered, pdfmake collects all tagged items, records which page each one lands on, and fills in the toc node you placed in the content. No separate pass or post-processing is needed.
The toc Node
Place a toc node anywhere in your content array — typically near the beginning, after a cover page. The node contains a toc object with optional configuration:
toc Configuration Properties
A pdfmake content node rendered as the heading above the TOC entries. Accepts any text node including styles.
Inline style overrides applied to the page-number column of each TOC entry. For example,
{ bold: true }.Inline style overrides applied to the text (title) column of each TOC entry. For example,
{ italics: true }.Four-element margin array
[left, top, right, bottom] applied to the text column. Defaults to [0, 0, 0, 0].Controls the order of TOC entries. Use
'page' (default) to sort by page number, or 'title' to sort alphabetically by the entry text.A BCP 47 locale string (e.g.
'cs', 'de') used when sorting by title. Enables locale-aware collation for non-English content.When
true, TOC entries are also added as PDF document outlines (bookmarks in the viewer sidebar).Marking Items for the TOC
To include a content node in the table of contents, addtocItem: true to it:
tocItem: true and uses it to populate the nearest toc node.
Styling Individual TOC Entries
EachtocItem node can carry its own per-entry style overrides that apply only to that row in the TOC:
Inline text styles applied to this entry’s text column in the TOC (e.g.
{ italics: true }).Four-element margin for this entry’s text column in the TOC.
Inline text styles applied to this entry’s page-number column in the TOC.
Using tocItem on Inline Text
tocItem can be applied to inline text spans nested inside a paragraph. Only the marked span contributes to the TOC — any surrounding text in the same paragraph is ignored.
Multiple TOCs with Named IDs
You can have more than one table of contents in a single document by giving eachtoc node an id and tagging content nodes with tocItem: 'id'.
Complete Working Example
The following example is taken directly fromexamples/toc.js in the pdfmake source repository. It demonstrates a document with one TOC, multiple heading levels, per-entry custom styles, and an inline tocItem.
pdfmake computes the TOC in a two-pass layout. If very long documents cause page numbers to shift during layout, the TOC will still be correct — the final page numbers are filled in after the full layout is complete.