Skip to content
Lamina PDF
0.9.0
GitHub
Open the web app

Pages that flow

This guide lays out a document whose length you don’t know in advance, such as a report or a contract. You give Lamina PDF blocks and a page template, and it makes as many pages as they need, with a header and a footer on each.

It keeps headings with their text and never leaves a single line of a paragraph alone at a page’s foot or head, and it tags the result so screen readers can follow it. In Rust you build the blocks with types such as Flow, Paragraph and Table. In TypeScript you give it the same document as plain objects, or as Markdown; the JSON section explains the format.

The example builds a 13-page report: a cover with a contents page, then five sections of headings, paragraphs, a footnote, a bulleted list and a photo. One paragraph is in Arabic and one in Hindi, and each is split by a page break. Every page after the cover names its section in the header and says “Page 3 of 13” in the footer, and every section has a bookmark. The report is built under PDF/UA-1, the accessibility standard, so the build fails if the output breaks one of its rules.

The TypeScript version describes the same report in plain objects, in the laminapdf.document/1 format, with a named style for each kind of text. It passes the fonts and the photo beside the document, by the names the document uses. Both languages write the same bytes:

packages/sdk/examples/flowing-report.ts
// A report that flows across pages: a cover with a contents page, then sections whose
// paragraphs break between pages, footnotes at the foot of their page, the section's title in
// a running header and "Page X of Y" in the footer.
//
// Lines split with at least two at a page's foot and head, headings stay with what follows, and the
// photo moves whole. The document is plain objects; its fonts and photo come beside it by name.
import { type BlockSpec, Builder, type BuilderOptions, type FlowDocument, Font, Image } from "@laminapdf/sdk";
const TITLE = "Reading files offline: a year of the reader";
const ARABIC = "يعمل القارئ دون اتصال بالشبكة، فتُفتح الملفات وتُقرأ وتُبحث على الجهاز نفسه، ولا يغادر أي منها الحاسوب.";
const HINDI = "नमस्ते दुनिया। हिन्दी पाठ का परीक्षण।";
const TOPICS = ["Opening files", "Search", "Printing", "Signing", "What comes next"];
const PHOTO_WIDTH = 200;
const PARAGRAPH_SPACE = 8;
const FOOTER = { center: "Page {page} of {pages}" };
/** The report's fonts: Latin, a collection whose second face is the bold one, Arabic and Devanagari. */
export type FlowingReportFonts = Readonly<Record<"regular" | "bold" | "arabic" | "devanagari", Uint8Array>>;
/**
* The report's bytes. Needs `init({ text: true })`.
*
* @param fonts - the fonts to set it in.
* @param photo - the JPEG each section shows.
* @param options - can fix the file ID, the producer and the creation date.
*/
export function flowingReport(
fonts: FlowingReportFonts,
photo: Uint8Array,
options: BuilderOptions = {},
): Uint8Array<ArrayBuffer> {
using b = new Builder({ ...options, profiles: ["PDF/UA-1"] });
b.flow(report(), {
fonts: {
regular: Font.fromBytes(fonts.regular),
arabic: Font.fromBytes(fonts.arabic),
devanagari: Font.fromBytes(fonts.devanagari),
bold: Font.fromCollection(fonts.bold, 1),
},
images: { photo: Image.jpeg(photo) },
});
using doc = b.finish();
return doc.save();
}
/** The whole report: a cover with the contents, then a section per topic. */
function report(): FlowDocument {
const body = { font: "regular", size: 11, fallbacks: ["arabic", "devanagari"] };
return {
schema: "laminapdf.document/1",
title: TITLE,
lang: "en",
// The cover is a plain A5 page; the others name their section and their number.
page: { size: "A5", first: { size: "A5" }, style: "small", header: { end: "{section}" }, footer: FOOTER },
styles: {
body,
note: { ...body, size: 9 },
arabic: { ...body, language: "ar" },
hindi: { ...body, language: "hi" },
heading: { font: "bold", size: 16 },
cover: { font: "bold", size: 24 },
small: { font: "regular", size: 9 },
},
blocks: [
{ type: "paragraph", text: TITLE, style: "cover" },
{ type: "paragraph", text: "A report from the reader's team." },
{ type: "contents" },
{ type: "pageBreak" },
...TOPICS.flatMap(section),
],
};
}
/** A section: its heading, paragraphs (one in Arabic, one in Hindi), a list and a photo. */
function section(topic: string, i: number): BlockSpec[] {
const sentence = `The ${topic.toLowerCase()} work of the year changed how readers use the app every day. `;
const note = { text: `Measured on our test files for ${topic}.`, style: "note" };
const paragraph = (repeats: number, withNote = false): BlockSpec => ({
type: "paragraph",
text: sentence.repeat(repeats),
spaceAfter: PARAGRAPH_SPACE,
spans: withNote ? [{ note }] : null,
});
const paragraphs = [paragraph(4), paragraph(5), paragraph(6), paragraph(7, true)];
const foreign: BlockSpec[] = []; // Section 2 has a paragraph in Arabic, section 4 one in Hindi.
if (i === 1) foreign.push({ type: "paragraph", text: `${ARABIC} `.repeat(8).trimEnd(), style: "arabic" });
if (i === 3) foreign.push({ type: "paragraph", text: `${HINDI} `.repeat(12).trimEnd(), style: "hindi" });
const items = ["faster", "smaller", "easier to share"].map((word) => ({ text: `Files are ${word}.` }));
const alt = `A photo of the team working on ${topic.toLowerCase()}`;
return [
{ type: "heading", level: 1, text: topic, style: "heading" },
...paragraphs,
...foreign,
{ type: "list", kind: "bullet", items },
{ type: "figure", image: "photo", alt, width: PHOTO_WIDTH },
];
}
Page 1 of flowing_report.pdf
Open flowing_report.pdf (13 pages, 57 KB)

The result is thirteen A5 pages: open the reference PDF.

A Flow is a list of blocks and the PageTemplate they flow through.

  • Paragraphs are made of pieces of text, each in its own TextStyle: its fonts, size, colour, language and a baseline rise for superscripts. A bold word is a piece in the bold font. A link is a piece with a target, and a line break or a footnote is a piece of its own.
  • Tables have header rows, body rows and footer rows; a later section covers them.
  • A contents block lists the headings with their pages, and footnotes go in paragraphs; both are covered below.
  • Headings run from level 1 to 6. Lists are bulleted or numbered, and an item can hold a nested list. A figure is an image with its alternate text, as wide as the page’s text column unless you give it a width.
  • Spacers, page breaks and keep groups shape the layout: a spacer adds space, a page break starts a new page, and a Keep group keeps its blocks on one page.

The template gives the page size, the margins, a header and a footer, and optionally a different first page, such as a cover without a header. A header or footer has three slots, at the start, centre and end of the line.

The flow lays out every page before it draws any, and it follows these rules:

  • Paragraphs split only between lines, leaving at least two lines at the foot of a page and two at the head of the next. When a page has room for only one line of a paragraph, the paragraph moves to the next page. Change the counts with with_widows_and_orphans, or a JSON paragraph’s widows and orphans.
  • A heading stays with the block after it, so a section never starts on the last line of a page. Any paragraph can do the same with with_keep_with_next, or keepWithNext in JSON.
  • Figures and keep groups move whole to the next page when they don’t fit.
  • A paragraph continued on a page of another width is broken again at that width.

When blocks you asked to keep together are taller than a page, the flow splits them anyway and adds a flow-keep-broken warning to the report Builder::flow returns. A figure or a single line taller than a page fails with flow-content-too-large, since no page could hold it.

A table can run over as many pages as its rows need, with its header on each page and every row whole. The second example builds a three-page invoice of 60 rows with a total at the end. Again, both languages write the same bytes:

packages/sdk/examples/flowing-invoice.ts
// An invoice whose table runs over three pages: its header row repeats at the top of each
// page, no row is split between pages, and the total stays on the page of the last row.
//
// The columns fit their widest cell, and numbers line up on the right. The document is plain
// objects in the `laminapdf.document/1` format, which names its two fonts; their bytes come beside
// it. The output is tagged (one `Table` element across the pages) and checked against PDF/UA-1.
import { type BlockSpec, Builder, type BuilderOptions, type CellSpec, type FlowDocument, Font } from "@laminapdf/sdk";
const TITLE = "Invoice 2026-114";
const PRODUCTS: readonly (readonly [string, number])[] = [
["Paper, A4, 80 g, box of 2,500 sheets", 2_450],
["Toner cartridge, black", 6_900],
["Desk lamp with a weighted base and an adjustable arm", 4_275],
["Stapler", 1_290],
["Archive boxes, set of ten, with lids", 3_150],
["Ballpoint pens, box of fifty", 1_875],
];
const LINES = 60;
const MARGIN = 56;
const PADDING = 6;
const RULE_WIDTH = 0.75;
const ROW_RULE_WIDTH = 0.25;
/** The frame and the rule under the header, the rules between rows, and the header row's shade. */
const RULE = "#666666";
const ROW_RULE = "#b3b3b3";
const HEADER_SHADE = "#ebebeb";
/**
* The invoice's bytes. Needs `init({ text: true })`.
*
* @param regular - the body's font.
* @param bold - a collection whose second face (index 1) is the bold one.
* @param options - can fix the file ID, the producer and the creation date.
*/
export function flowingInvoice(
regular: Uint8Array,
bold: Uint8Array,
options: BuilderOptions = {},
): Uint8Array<ArrayBuffer> {
using b = new Builder({ ...options, profiles: ["PDF/UA-1"] });
const document: FlowDocument = {
schema: "laminapdf.document/1",
title: TITLE,
lang: "en",
page: { size: "A4", margins: MARGIN, footer: { center: "{title}, page {page} of {pages}" }, style: "small" },
styles: {
body: { font: "regular", size: 10 },
strong: { font: "bold", size: 10 },
title: { font: "bold", size: 18 },
small: { font: "regular", size: 8 },
},
blocks: [
{ type: "heading", level: 1, text: TITLE, style: "title" },
{ type: "paragraph", text: "Office supplies delivered in October, to be paid within 30 days." },
table(),
],
};
b.flow(document, { fonts: { regular: Font.fromBytes(regular), bold: Font.fromCollection(bold, 1) } });
using doc = b.finish();
return doc.save();
}
/** Sixty lines of products, a header row and a footer row with the total. */
function table(): BlockSpec {
const number = (text: string, style = "body"): CellSpec => ({ text, style, align: "end" });
const header = ["Item", "Description", "Qty", "Amount"].map(
(text, i): CellSpec => ({ text, style: "strong", ...(i >= 2 ? { align: "end" } : {}) }),
);
let total = 0;
const rows = Array.from({ length: LINES }, (_, i) => {
const [name, cents] = PRODUCTS[i % PRODUCTS.length] ?? ["", 0];
const qty = (i % 5) + 1;
total += qty * cents;
return {
cells: [
{ text: String(i + 1).padStart(3, "0") },
{ text: name },
number(String(qty)),
number(money(qty * cents)),
],
};
});
return {
type: "table",
columns: ["auto", "auto", "auto", "auto"],
padding: PADDING,
frame: { width: RULE_WIDTH, color: RULE },
headerRule: { width: RULE_WIDTH, color: RULE },
rowRules: { width: ROW_RULE_WIDTH, color: ROW_RULE },
headerBackground: HEADER_SHADE,
header: [{ cells: header }],
rows,
footer: [{ cells: [{ text: "Total", style: "strong", span: 3 }, number(money(total), "strong")] }],
};
}
function money(cents: number): string {
return `${Math.floor(cents / 100)}.${String(cents % 100).padStart(2, "0")}`;
}
Page 1 of flowing_invoice.pdf
Open flowing_invoice.pdf (3 pages, 27 KB)

The result is three A4 pages: open the reference PDF.

A table follows three rules when it crosses a page:

  • The header rows repeat at the top of every page the table continues on.
  • A body row never splits. A row that doesn’t fit what is left of a page starts the next one, and a row taller than a page fails with flow-content-too-large.
  • The footer rows stay with the last body row, so a total never starts a page alone. When the footer doesn’t fit under the last row, that row moves to the next page with the footer.

Columns get their widths once, for the whole table, so they line up from page to page. A column is Fixed (a width in points), Auto (as wide as its widest cell) or a Fraction (a share of the width the others leave). When automatic columns are too wide together, the widest ones narrow first, so a column of numbers keeps its width while the descriptions wrap.

A cell holds one or more paragraphs. with_align lines its text up at the start, centre or end (numbers at the end), and with_vertical_align places it in the row. with_span stretches a cell over several columns, as the invoice’s “Total” does. Rules come from with_frame, with_header_rule and with_row_rules, and with_header_background shades the header rows. In a JSON document, as in the TypeScript invoice, a cell takes align, valign and span. The table takes padding, frame, headerRule, rowRules and headerBackground, with colours written as #rrggbb.

A footnote is part of a paragraph: Paragraph::note(body) puts a raised number where you add it and the note’s body at the foot of the same page. In JSON, a span’s note does the same. Notes are numbered 1, 2, 3 through the flow. A short rule separates them from the text above, and the page’s footer stays below them. When a line’s notes don’t fit at the foot of its page, the line moves to the next page with them.

A contents block, FlowBlock::Contents, lists the flow’s headings with the page each one starts on. Each entry is indented by its heading’s level, with dots leading to its number, and the entry links to its heading. By default it lists levels 1 and 2; Contents::with_levels changes that, as does levels in JSON. The report in the example puts its contents on the cover.

The flow can’t know the page numbers before it has laid the pages out. So it lays everything out, writes the numbers it found into the contents page, and lays out again to check them. Each entry keeps room for a number of up to four digits, so the contents page keeps its length and the second layout confirms the numbers.

Every heading also gets a bookmark. PDF readers list the bookmarks in their sidebar, nested by heading level, and each one goes to its heading. Turn them off with Flow::with_outline(false), or outline: false in JSON.

You can also write the document in Markdown and let Builder::markdown turn it into blocks. Each Markdown construct becomes the block you would build by hand: a # heading becomes a heading, a list becomes a list, and a table becomes a table that runs across pages, its header row repeated on each. A link to #a-heading goes to the heading of that name, written the way GitHub writes it: lower case, with hyphens for spaces. Front matter at the top, plain key: value lines between two --- lines, sets the document’s title, language, author and subject.

The example below builds a short report from a Markdown file. It gives the fonts, the page template and a contents page in MarkdownBuildOptions, and it passes the one image the Markdown names. The builder reads no files and fetches nothing, so it leaves out an image you don’t pass and adds a markdown-image-missing warning. It leaves out raw HTML too, with markdown-html-ignored. Both warnings give the position in the Markdown, never its text.

In TypeScript, builder.markdown(text, options) takes the same options, with the fonts as Fonts and the images as Images by the name the Markdown uses. It needs the text module, init({ text: true }). Both languages write the same bytes:

packages/sdk/examples/markdown-to-pdf.ts
// A report written in Markdown, turned into tagged pages that flow: headings, paragraphs with
// emphasis, code and links, a photo, lists and a task list, a block quote, a table that runs
// across pages, a code block, footnotes, and paragraphs in Arabic and Hindi.
//
// The front matter gives the title and language, a contents page lists the headings, and the
// output meets PDF/UA-1, so every font is embedded. The builder reads no files: the fonts and the
// one photo the Markdown names are passed in, and nothing is fetched.
import { Builder, type BuilderOptions, Font, Image } from "@laminapdf/sdk";
/** The fonts the report is set in: Latin, a bold face, Arabic and Devanagari. */
export interface ReportFonts {
readonly regular: Uint8Array;
/** A collection whose second face (index 1) is the bold one. */
readonly bold: Uint8Array;
readonly arabic: Uint8Array;
readonly devanagari: Uint8Array;
}
/**
* The report's bytes. Needs `init({ text: true })`.
*
* @param markdown - the report's Markdown.
* @param fonts - the fonts to set it in.
* @param photo - the JPEG the Markdown shows as `photo.jpg`.
* @param options - can fix the file ID, the producer and the creation date.
*/
export function markdownToPdf(
markdown: string,
fonts: ReportFonts,
photo: Uint8Array,
options: BuilderOptions = {},
): Uint8Array<ArrayBuffer> {
using b = new Builder({ ...options, profiles: ["PDF/UA-1"] });
const report = b.markdown(markdown, {
fonts: {
regular: Font.fromBytes(fonts.regular),
bold: Font.fromCollection(fonts.bold, 1),
fallbacks: [Font.fromBytes(fonts.arabic), Font.fromBytes(fonts.devanagari)],
},
images: { "photo.jpg": Image.jpeg(photo) },
// A5 pages, so the table runs across two of them, with the page number in the footer.
page: { size: "A5", footer: { center: "Page {page} of {pages}" }, furnitureSize: 8 },
contents: 2,
});
using doc = b.finish();
const [warning] = doc.warnings;
if (warning !== undefined) throw new Error(`the Markdown has something the PDF leaves out: ${warning.code}`);
if (report.pages.length === 0) throw new Error("the report has no pages");
return doc.save();
}
Page 1 of markdown_to_pdf.pdf
Open markdown_to_pdf.pdf (4 pages, 52 KB)

The result is four A5 pages: open the reference PDF.

The laminapdf command line does the same without code. It reads the images from the Markdown’s folder and the fonts from --font:

Terminal window
laminapdf build report.md -o report.pdf --font regular=NotoSans-Regular.ttf --profile pdf-ua-1

If you pass no fonts, the builder sets the Markdown in Helvetica and Courier, two fonts every PDF reader has. They set only Latin text, and since they aren’t embedded, a PDF/UA-1 build refuses them with font-not-embedded. Pass your own fonts with MarkdownFonts for other scripts or an accessible document.

You can also describe the document as JSON, in the format laminapdf.document/1, and lay it out with Builder::document. The JSON names what you would build by hand. A page sets the size, margins, header and footer, styles each name a font and a size, and blocks are tagged by type, such as paragraph, heading, list, table, figure and contents. Fonts and images are named in the document, and their bytes come beside it in Resources, so the JSON holds no files.

A format with a schema suits programs and language models: they write the document, and the schema checks it before anything is drawn. Lamina PDF reads only the keys the format defines. A misspelt one fails with invalid-document, whose data.pointer names the value and whose didYouMean suggests the key it was close to.

A template is the same JSON with placeholders that fill replaces from your data. $data puts in a value and $each repeats a block or table row per item, with $item for a value of the current item. $if keeps a block only when a value is there. The Fill an invoice from JSON recipe builds a two-page invoice this way.

In TypeScript, builder.flow(document, { data, fonts, images }) takes the same document as plain objects, with the fonts as Fonts and the images as Images by name. The package’s types follow the format, so your editor flags a misspelt key or an unknown block type before anything runs. Type a document as FlowDocument, and a template with placeholders as FlowTemplate. Given data, flow fills the template first. It needs the text module, init({ text: true }).

A slot’s text can hold fields, which the flow fills in on each page:

  • {page}: the page’s number in the document, from 1.
  • {pages}: the number of pages in the document once the flow’s pages are added.
  • {section}: the title of the last level-1 heading that started on or before the page. Choose another level with with_section_level.
  • {title}: the title you gave the flow.

Write {{ and }} for a literal brace. {pages} is right on every page because no page is drawn until all of them are laid out.

The output is tagged unless you turn tagging off with with_tagged(false). Paragraphs are P elements, headings H1 to H6, lists L with their items, figures Figure with their alternate text, and links Link. A paragraph’s language, taken from its first piece, is set on its element; a piece in another language is a Span with that language. A paragraph split across pages stays one element, so a screen reader reads it as one.

A table is one Table element however many pages it crosses, with THead, TBody and TFoot around its rows. Header cells are TH elements that head their columns, and a cell across several columns records how many it spans.

The header rows drawn again on later pages are marked as page furniture, like headers and footers, so a screen reader and Lamina PDF’s extraction read the table’s header once. The table’s rules and background are marked as decoration too.

A footnote’s number is a Reference and its body a Note with its own identifier, inside the paragraph that cites it. A contents page is a TOC element with a TOCI for each entry, each holding its links; its dots are marked as decoration.

Headers and footers are marked as page furniture rather than content: screen readers skip them, and Lamina PDF’s own extraction leaves them out of the text.

  • A cover: give the template a first page with with_first_page, and start the body with a page break.
  • Another typeface for a word: add a piece of text in a TextStyle with that font. List fallback fonts with with_fallback for characters the first font lacks.
  • Page numbers only: pass a footer with one centre slot, "{page}", and no header.

Next: Accessible PDFs, for what PDF/UA-1 checks and how to meet it.