Skip to content
Lamina PDF
0.9.0
GitHub
Open the web app

Element flow

You’ll lay out a report where each part starts below wherever the part before it ended: a title, a summary, optional notes, a photo and a body that runs onto a second page. Use it when the length of your text changes from one document to the next.

The function takes a JPEG’s bytes and the report’s text, and returns the PDF’s bytes. In TypeScript it draws text, so it needs init({ text: true }). Both languages write the same bytes:

packages/sdk/examples/element-flow.ts
// A report whose title, paragraphs and photo each start below wherever the one before ended. Each
// text box reports its `bottom`, and the next element starts a gap below it, so a summary that
// wraps moves everything after it and empty notes take no space. The body continues on new pages
// from the top margin. Needs `init({ text: true })`.
import { Builder, type BuilderOptions, Font, Image, PAGE_SIZES, type Surface, type TextLayout } from "@laminapdf/sdk";
const [PAGE_WIDTH, PAGE_HEIGHT] = PAGE_SIZES.A5;
const MARGIN = 48;
const WIDTH = PAGE_WIDTH - 2 * MARGIN;
const BOTTOM = PAGE_HEIGHT - MARGIN;
const TITLE = 18;
const BODY = 11;
const GAP = 10;
/** The photo's height, for a 3:2 photo across the text column. */
const PHOTO_HEIGHT = WIDTH / 1.5;
/** What the report says. Empty `notes` take no space; blank lines separate the body's paragraphs. */
export interface Report {
readonly title: string;
readonly summary: string;
readonly notes: string;
readonly body: string;
}
/**
* The report's bytes, with `photo` (a JPEG) below its notes.
*
* @param options - can fix the file ID, the producer and the creation date.
*/
export function elementFlow(photo: Uint8Array, report: Report, options: BuilderOptions = {}): Uint8Array<ArrayBuffer> {
using b = new Builder(options);
const fonts = { bold: Font.standard("Helvetica-Bold"), regular: Font.standard("Helvetica") };
const jpeg = Image.jpeg(photo);
let page = b.page("A5");
let y = header(page.surface(), fonts, jpeg, report);
let [rest, newPage] = [report.body.trimEnd(), false];
for (;;) {
const layout = page.surface().textBox(rest, { font: fonts.regular, size: BODY, box: area(y) });
// A clipped box still drew its first lines: the rest goes on a new page, from its top.
const cut = layout.overflow;
if (cut === undefined) break;
if (cut === 0 && newPage) throw new Error("a line of the body is taller than the page");
[rest, y, newPage] = [rest.slice(cut).trimStart(), MARGIN, true];
page.finish();
page = b.page("A5");
}
page.finish();
using doc = b.finish();
return doc.save();
}
/**
* The title with a rule under it, the summary, the notes and the photo, each below the one before;
* returns where the body starts. Text that doesn't fit throws `text-overflow`.
*/
function header(s: Surface, fonts: { bold: Font; regular: Font }, photo: Image, report: Report): number {
const fit = (text: string, font: Font, size: number, top: number) =>
below(s.textBox(text, { font, size, box: area(top), overflow: "error" }), top);
let y = fit(report.title, fonts.bold, TITLE, MARGIN);
// A decoration half a gap above the summary. It's stroked: a fill colour would colour the text too.
s.strokeColor([0.6])
.path((p) => p.rect([MARGIN, y - GAP / 2, WIDTH, 0]))
.stroke();
for (const text of [report.summary, report.notes]) y = fit(text, fonts.regular, BODY, y);
if (y + PHOTO_HEIGHT > BOTTOM) throw new Error("the photo doesn't fit below the notes");
s.image(photo, [MARGIN, y, WIDTH, PHOTO_HEIGHT]);
return y + PHOTO_HEIGHT + GAP;
}
/** Where the next element starts: a gap below `layout`'s last drawn line, or `y` if it drew none. */
function below(layout: TextLayout, y: number): number {
return layout.bottom === undefined ? y : layout.bottom + GAP;
}
/** The rest of the page's text column, from `y` down to the bottom margin. */
function area(y: number): [number, number, number, number] {
return [MARGIN, y, WIDTH, Math.max(BOTTOM - y, 0)];
}
Page 1 of element_flow.pdf
Open element_flow.pdf (2 pages, 6 KB)

The result is two A5 pages. The body fills the first page below the photo and finishes at the top of the second. The Result tab shows it, with a link to the PDF.

Every text box tells you where its text ended, so you never type a position for anything after the first element.

  • bottom is where the next element can start. textBox returns it with the lines it drew: the bottom of the last line that draws something. Rust has TextLayout::bottom(), and TextRun::bottom() for a single run of text.
  • Empty text has no bottom. The notes here are optional. When they’re empty, bottom is undefined (None in Rust), below keeps y where it was, and the photo moves up with no gap left behind.
  • A longer summary moves everything after it. When the summary wraps onto one more line, the notes, the photo and the body each move down by one line height, and none of the code changes.
  • The photo is placed by your own rectangle. An image reports nothing back, so the code adds the photo’s height and the gap to y itself.
  • The rule under the title is a decoration. It’s drawn half a gap above the summary and leaves y alone. It’s stroked rather than filled: a fill colour stays set and would colour the text drawn after it.

The report treats overflow two ways, and you choose which for each element.

The title, summary and notes must fit on the first page. They’re drawn with overflow: "error" (Overflow::Error in Rust), so text that doesn’t fit throws text-overflow instead of disappearing. The photo must fit too: when it would cross the bottom margin, the code stops with an error of its own.

The body may run on. Its box reaches the bottom margin and clips what doesn’t fit. Then overflow holds the offset of the first character it didn’t draw: a UTF-16 offset in TypeScript, a byte offset in Rust. The loop starts a new page and draws the rest from the top margin. Test overflow with !== undefined, because 0 means not even one line fitted. If no line fits even on a fresh page, a line is taller than the page, so the loop stops with an error instead of adding pages for ever.

bottom is a typographic measurement in points: the descent of the last drawn line’s font, below its baseline. A glyph that reaches lower, or a thick stroke around the text, can paint past it.

Like every position, bottom is in page space, measured from the top of the page. It’s measured before the surface’s transform, so inside transform it stays in the coordinates you drew in.

In TypeScript you pass a box as a tuple, [x, y, width, height]. The lines a text box returns describe their rect as an object with x, y, width and height.

Page breaks stay your decision: the code here makes each one from overflow. The builder’s surface doesn’t paginate, float an image beside text, or lay out tables. Pages that flow paginates for you, in both languages: headings kept with their text, running headers and “Page X of Y” footers.

  • Paragraphs a gap apart: draw each paragraph in its own text box and start the next one at bottom plus the gap, as the summary and notes do.
  • A line height for the body: pass lineHeight (with_line_height in Rust); bottom follows it.
  • Two columns: keep a y for each column, and when the left column fills, carry on at the top margin of the right one.

Next: the PDF/UA report, a report that screen readers can follow.