Skip to content
Lamina PDF
0.9.0
GitHub
Open the web app

Using Builder

A Builder makes a new PDF: you add pages, draw on them, and finish into a Document you save. Each task below has a short example and a link to the guide that goes further. The Builder reference lists every method and option.

b.page("A4") adds a page, page.surface() is what you draw on, and page.finish() writes the page into the document. b.finish() then returns the new document, and doc.save() its bytes:

packages/sdk/examples/hello.ts
// The smallest document: one A4 page with a line of text in a standard 14 font.
import { Builder, type BuilderOptions, Font } from "@laminapdf/sdk";
/**
* The document's bytes. Needs `init({ text: true })`.
*
* @param options - can fix the file ID, the producer and the creation date, so that the same
* input gives the same bytes on every run.
*/
export function hello(options: BuilderOptions = {}): Uint8Array<ArrayBuffer> {
using b = new Builder(options);
const page = b.page("A4");
// Page space: points from the top-left corner, y down; `at[1]` is the text's baseline.
page.surface().text("Hello, world!", { font: Font.standard("Helvetica"), size: 24, at: [72, 96] });
page.finish();
using doc = b.finish();
return doc.save();
}
Page 1 of hello.pdf
Open hello.pdf (1 page, 1 KB)

Positions are in points (1/72 inch) from the page’s top-left corner, with y going down. Pages and coordinates has the details, rotated pages included.

Call b.page() once per page, and finish each page before the builder. This example gives each page a heading and a “Page 2 of 3” footer:

packages/sdk/examples/page-numbers.ts
// A document of several pages, each with a heading and a footer that reads "Page 2 of 3". A page
// takes no more drawing once it is finished, so the page count is known before the first page is
// drawn.
import { Builder, type BuilderOptions, Font, PAGE_SIZES } from "@laminapdf/sdk";
const MARGIN = 72;
const HEADING_SIZE = 20;
const FOOTER_SIZE = 10;
const PAGE_SIZE = "A4";
const [, PAGE_HEIGHT] = PAGE_SIZES[PAGE_SIZE];
/**
* One page per heading in `headings`. Needs `init({ text: true })`.
*
* @param options - can fix the file ID, the producer and the creation date.
*/
export function pageNumbers(headings: readonly string[], options: BuilderOptions = {}): Uint8Array<ArrayBuffer> {
using b = new Builder(options);
const font = Font.standard("Helvetica");
headings.forEach((heading, index) => {
const page = b.page(PAGE_SIZE);
const s = page.surface();
// `at` is the baseline's start, in points from the page's top-left corner.
s.text(heading, { font, size: HEADING_SIZE, at: [MARGIN, MARGIN + HEADING_SIZE] });
const footer = `Page ${index + 1} of ${headings.length}`;
s.text(footer, { font, size: FOOTER_SIZE, at: [MARGIN, PAGE_HEIGHT - MARGIN / 2] });
// A page joins the document only once it is finished.
page.finish();
});
b.metadata({ title: headings[0] ?? "Untitled" });
using doc = b.finish();
return doc.save();
}

When you don’t know how many pages the content needs, let the builder decide: pages that flow breaks headings, paragraphs and tables across pages, with headers and footers, and b.markdown(text) does the same for a Markdown file.

  • surface.text(str, { font, size, at }) draws text as it is, starting a new line only at each line break in it, and surface.textBox(str, { font, size, box }) wraps a paragraph to fit a box. Text and fonts covers fonts, scripts and text that doesn’t fit.
  • surface.path(p => …) draws a shape you then fill, stroke or clip to, and surface.image(image, rect) places a JPEG, a PNG or raw pixels. Shapes, colour and images covers colour, opacity and transforms.

surface.link(rect, target) makes a region of a page link to another page or a web address. On the builder, b.outline(entries) adds bookmarks, b.pageLabels(ranges) numbers pages i, ii, 1, 2, and b.metadata({ title }) and b.lang("en") describe the document. Links, outlines and metadata has the example.

new Builder({ profiles: ["PDF/UA-1"] }) makes a document for screen readers, and "PDF/A-2b" one for archives. Under a profile, b.finish() checks the rules a program can check and fails with the code of the first one the document breaks. Accessible PDFs and archival PDFs list the rules.

  • Text needs the text module. Load it with init({ text: true }) before you create the builder. Without it, a builder draws shapes and images only, and text fails with text-module-not-loaded.
  • at is the baseline. at: [x, y] places the start of the line the letters sit on, so the letters rise above y. Text at y: 0 is drawn above the page’s top edge, out of sight.
  • An unfinished page is left out. A page joins the document only when you call page.finish(). Pages still open when the builder finishes are dropped.
  • A finished page takes no more drawing. Draw everything a page needs, such as a footer with the page count, before you finish it. Drawing after that fails with builder-finished.
  • Some mistakes show up late. A shape with invalid values fails at page.finish() with invalid-geometry. A link to a page or destination that doesn’t exist fails at b.finish(), as does a profile’s broken rule.
  • The standard 14 fonts are Latin only. Font.standard("Helvetica") needs no font file, but it covers Western European text and isn’t embedded, so PDF/UA and PDF/A refuse it with font-not-embedded. Load your own font with Font.fromBytes(bytes) for other scripts and for those profiles.
  • Dates come from you. Lamina PDF never reads the clock, so a document has a creation date only when you pass creationDate. The same content and options give the same bytes on every run; test the PDFs you generate builds on that.
  • Free the builder and the document. b.finish() frees the builder, but a builder that throws before it finishes, such as on a missing glyph, holds its memory until the garbage collector reaches it. Declare it with using b = new Builder(), as the examples do, so it is freed either way. The document b.finish() returns holds its own memory, so write using doc = b.finish() too.

Next: the Builder reference.