Production, on one page
This page shows every Production page in order. Read offline lists every section and the whole guide as one PDF.
Keep the page responsive
You’ll run Lamina PDF in a Web Worker, so opening or saving a large file never freezes your page. You need this in a browser app once files get big: on the main thread, a full rewrite of a 50 MB file blocks the page for about 200 ms.
@laminapdf/sdk is synchronous after init(). @laminapdf/sdk/worker runs the same API in a Web Worker, where you call the same methods and each returns a Promise. This example opens a file in a Web Worker and saves it there. The save reports its progress, and aborting the signal stops the work, for example when the user presses Cancel:
// Saving a PDF in a Web Worker, so a large file never blocks the page: `@laminapdf/sdk/worker`// runs the same API there, and each method returns a Promise. The save reports its progress step// by step, and aborting the signal (a Cancel button, say) stops the work.import { type CallOptions, connect } from "@laminapdf/sdk/worker";
/** What the save reports to and listens to. */export type SaveInWorkerOptions = Pick<CallOptions, "onProgress" | "signal">;
/** * Opens `bytes` in a Web Worker and saves them there as a full rewrite. * * @param bytes - any PDF; it is copied to the worker (`transfer: true` would move it). * @param options - `onProgress` is called after each step of the save. Aborting `signal` stops * the work wherever it is (starting the Web Worker, opening, or at the save's next step), and * the promise rejects with a `cancelled` error. * @returns the saved file. */export async function saveInWorker(bytes: Uint8Array, options: SaveInWorkerOptions = {}): Promise<Uint8Array> { const client = await connect(options); try { await using doc = await client.open(bytes, options); return await doc.save({ mode: "fullRewrite" }, options); } finally { client.terminate(); }}Call the same methods, awaited
Section titled “Call the same methods, awaited”Getters become methods: await doc.pageCount(). doc.page(i) returns a remote page, and page.text() returns a PageText, once you’ve called client.loadExtract().
The Web Worker leaves out a few members, each for a reason:
transact: its callback can’t cross to the Web Worker, so pass the edits toapply(ops)instead.saveJob: the Web Worker’ssavealready works in steps.ensure: the Web Worker fetches the byte ranges a call needs by itself.free: calldoc.close(), or useawait using, instead.Builder: build on the main thread, or in a Web Worker of your own.
Cancel a call
Section titled “Cancel a call”The Web Worker runs one call at a time, so a call waits while another runs. Pass an AbortSignal as signal to any call. An aborted call rejects with cancelled, and error.cause holds the signal’s reason. What happens to the work depends on where it was:
- A call that hasn’t started never runs.
saveandsaveWithReportstop at their next step, and the document stays usable.- Any other call runs to the end in the Web Worker, because it is one synchronous call, though the byte fetches of a file opened with
openUrlstop.client.terminate()stops it at once.
Your page's client sends each call, such as await doc.pageCount(), to the Web Worker as a message. There @laminapdf/sdk runs one call at a time, and each answer comes back as a Promise; saved bytes and page text come back transferred rather than copied. When a call's AbortSignal aborts, the call rejects with cancelled. A call not started never runs, and a running save or saveWithReport stops at its next step. Any other call runs to the end, though the byte fetches of a file opened with openUrl stop.
Move bytes instead of copying them
Section titled “Move bytes instead of copying them”Saved bytes and page text come back transferred, not copied. Your input is copied unless you pass transfer: true, which moves it and leaves your buffer empty.
Recover from a crash
Section titled “Recover from a crash”If the Web Worker dies, pending calls reject with worker-crashed, and client.on("crashed", …) fires. client.restart() starts a fresh Web Worker, and you open your documents again.
connect() also checks that the Web Worker comes from the same @laminapdf/sdk build as your page. It rejects with worker-version-mismatch otherwise.
Bundlers and a content security policy
Section titled “Bundlers and a content security policy”Vite and webpack bundle the Web Worker with no setup. In Vite, exclude @laminapdf/sdk from optimizeDeps, as for the main entry. To host the Web Worker’s file yourself, for example under a strict content security policy, pass connect({ worker: () => new Worker(url, { type: "module" }) }).
Next: browse the recipes, with the PDF each one makes, or the troubleshooting page when something goes wrong.
Test the PDFs you generate
You’ll write a test that fails when a PDF your code makes changes. The test compares the PDF’s bytes with a reference PDF you opened and approved. When the bytes differ, a second example draws both files’ pages to show where they look different.
Why the bytes repeat
Section titled “Why the bytes repeat”Lamina PDF never reads a clock or picks a random value while it writes. With the same code, data, options and Lamina PDF version, Rust and every JavaScript runtime write the same bytes. Three values usually come from your app rather than from the content (the file ID, the producer and the creation date), so a test fixes them.
Your own code can vary the bytes too, and a failing test should point there first:
- a date taken from the clock, such as an invoice date or
new Date()in a footer; - records read in a different order, such as rows from a database without
ORDER BY; - a different font file: an update to the font changes the embedded subset.
Compare with a reference PDF
Section titled “Compare with a reference PDF”The example below holds the options a test passes and a function that compares a PDF with its reference. The function answers same, created or changed, and your test runner checks the answer, so it works in Vitest, Jest, node:test or cargo test:
// Checks that a PDF your code makes is byte for byte the reference PDF you approved.//// Build the document with `TEST_OPTIONS`, then let your test runner check the answer://// expect(compareWithReference(invoice(order, TEST_OPTIONS), "references/invoice.pdf")).toBe("same");//// The first run fails with `created` and writes the reference: open it, and commit it once it// looks right. When the bytes change later, the new PDF is written beside the reference for// review; pass `{ update: true }` to accept it.import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { basename, dirname, extname, join } from "node:path";import type { BuilderOptions } from "@laminapdf/sdk";
/** * What a test passes in place of the clock and a random file ID. With these fixed, the same code * and data give the same bytes on every run, machine and runtime. */export const TEST_OPTIONS: BuilderOptions = { // The file ID: any 32 hex digits, as long as they stay the same. id: "00112233445566778899aabbccddeeff", // The Info dictionary's /Producer: your app's name, without a version that changes. producer: "my-app tests", // The Info dictionary's /CreationDate, in PDF date form. creationDate: "D:20260101000000Z",};
/** `same`: the bytes match. `created`: there was no reference, so one was written. `changed`: they differ. */export type Outcome = "same" | "created" | "changed";
export interface CompareOptions { /** Replace the reference with the new bytes instead of writing them beside it. */ readonly update?: boolean;}
/** * Compares `bytes` with the reference PDF at `reference`. A changed PDF is written to * `<name>.new.pdf` next to the reference, or replaces it when `update` is set. */export function compareWithReference(bytes: Uint8Array, reference: string, options: CompareOptions = {}): Outcome { const pending = join(dirname(reference), `${basename(reference, extname(reference))}.new.pdf`); if (!existsSync(reference)) { mkdirSync(dirname(reference), { recursive: true }); writeFileSync(reference, bytes); return "created"; } if (sameBytes(readFileSync(reference), bytes)) { rmSync(pending, { force: true }); return "same"; } if (options.update) { writeFileSync(reference, bytes); rmSync(pending, { force: true }); } else { writeFileSync(pending, bytes); } return "changed";}
function sameBytes(a: Uint8Array, b: Uint8Array): boolean { return a.length === b.length && a.every((byte, i) => byte === b[i]);}//! Checks that a PDF your code makes is byte for byte the reference PDF you approved.//!//! Build the document with `test_options()`, then assert on the answer in a test://!//! ```ignore//! let bytes = invoice(&order, test_options()?)?;//! assert_eq!(compare_with_reference(&bytes, Path::new("references/invoice.pdf"), false)?, Outcome::Same);//! ```//!//! The first run fails with `Created` and writes the reference: open it, and commit it once it//! looks right. When the bytes change later, the new PDF is written beside the reference for//! review; pass `update: true` to accept it. Run on its own, the example compares two files://!//! ```sh//! cargo run -p laminapdf --example snapshot_test -- new.pdf reference.pdf//! ```
use std::error::Error;use std::path::{Path, PathBuf};
use laminapdf::BuildOptions;
fn main() -> Result<(), Box<dyn Error>> { let mut args = std::env::args().skip(1); let (Some(new), Some(reference)) = (args.next(), args.next()) else { return Err("usage: snapshot_test <new.pdf> <reference.pdf>".into()); }; let outcome = compare_with_reference(&std::fs::read(new)?, Path::new(&reference), false)?; println!("{outcome:?}"); Ok(())}
/// What a test passes in place of the clock and a random file ID. With these fixed, the same code/// and data give the same bytes on every run, machine and runtime.#[cfg_attr( not(test), expect(dead_code, reason = "your tests call it; `main` only compares two files"))]pub(crate) fn test_options() -> Result<BuildOptions, Box<dyn Error>> { /// The file ID: any 16 bytes, as long as they stay the same. const FILE_ID: [u8; 16] = [ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, ]; /// The Info dictionary's `/Producer`: your app's name, without a version that changes. const PRODUCER: &str = "my-app tests"; /// The Info dictionary's `/CreationDate`, in PDF date form. const CREATED: &str = "D:20260101000000Z"; Ok(BuildOptions::new() .with_id(FILE_ID) .with_producer(PRODUCER) .with_creation_date(CREATED.parse()?))}
/// `Same`: the bytes match. `Created`: there was no reference, so one was written. `Changed`:/// they differ.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub(crate) enum Outcome { Same, Created, Changed,}
/// Compares `bytes` with the reference PDF at `reference`. A changed PDF is written to/// `<name>.new.pdf` next to the reference, or replaces it when `update` is set.pub(crate) fn compare_with_reference(bytes: &[u8], reference: &Path, update: bool) -> std::io::Result<Outcome> { let pending = pending_path(reference); if !reference.exists() { if let Some(folder) = reference.parent() { std::fs::create_dir_all(folder)?; } std::fs::write(reference, bytes)?; return Ok(Outcome::Created); } if std::fs::read(reference)? == bytes { remove_if_present(&pending)?; return Ok(Outcome::Same); } if update { std::fs::write(reference, bytes)?; remove_if_present(&pending)?; } else { std::fs::write(&pending, bytes)?; } Ok(Outcome::Changed)}
/// `invoice.pdf` → `invoice.new.pdf`, in the same folder.fn pending_path(reference: &Path) -> PathBuf { let stem = reference.file_stem().unwrap_or_default().to_string_lossy(); reference.with_file_name(format!("{stem}.new.pdf"))}
fn remove_if_present(path: &Path) -> std::io::Result<()> { match std::fs::remove_file(path) { Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()), result => result, }}On the first run there is no reference yet, so the function writes one, answers created, and the test fails. Open that PDF before you commit it: the test only proves that later runs match it, so it has to be right. After that, every run answers same until the output changes. A reference you forget to commit fails the test on every other machine too, since a fresh checkout has none.
When the bytes change, the reference stays as it was and the new PDF goes beside it as <name>.new.pdf. Open both, and if the change is the one you meant, run the tests again with update set, for example from an environment variable such as UPDATE_REFERENCES=1. Commit the new reference with the change that caused it.
See where a changed PDF looks different
Section titled “See where a changed PDF looks different”Different bytes don’t always mean different pages: a reordered dictionary or a new producer changes the file but not how it looks. This example draws each page of both files and counts the pixels that differ:
// Draws each page of two PDFs and counts the pixels that differ, to show where a changed PDF looks different.//// Both files are drawn by the same renderer, so its own quirks cancel out. In Node, load it with// the module's bytes://// const wasm = fileURLToPath(import.meta.resolve("@laminapdf/wasm/lamina_pdf_render_hayro.wasm"));// const renderer = await loadRenderer(readFileSync(wasm));import { bytesSource, DEFAULT_LIMITS, type RenderBackend, type RenderDocument, type RenderedTile,} from "@laminapdf/render";import { initHayro } from "@laminapdf/render-hayro";
/** Pixels per point: 2 draws at 144 dpi, enough to see a glyph that moved. Pages too large for the renderer's limits at this scale are drawn smaller. */const SCALE = 2;/** How far a colour channel (0–255) may move before the pixel counts as changed: absorbs smoothing noise. */const TOLERANCE = 8;
/** A rectangle in page points, from the page's top-left corner. */export interface Box { readonly x: number; readonly y: number; readonly width: number; readonly height: number;}
/** One page: how many pixels differ, and the box around them (`null` when none do). */export interface PageDifference { readonly page: number; readonly pixels: number; readonly box: Box | null;}
export interface Comparison { /** The page counts of the first and second file; only the pages both have are compared. */ readonly pageCounts: readonly [number, number]; readonly pages: readonly PageDifference[];}
/** Loads the renderer from the module's bytes, a URL, or (in a browser) the file next to its code. */export function loadRenderer(wasm?: Uint8Array | URL): Promise<RenderBackend> { return initHayro(wasm);}
/** Compares every page the two PDFs share. */export function comparePages(renderer: RenderBackend, before: Uint8Array, after: Uint8Array): Comparison { using a = renderer.open(bytesSource(before)); using b = renderer.open(bytesSource(after)); const shared = Math.min(a.pageCount, b.pageCount); const pages = Array.from({ length: shared }, (_, page) => comparePage(a, b, page)); return { pageCounts: [a.pageCount, b.pageCount], pages };}
function comparePage(a: RenderDocument, b: RenderDocument, page: number): PageDifference { const scale = Math.min(fittingScale(a, page), fittingScale(b, page)); const first = a.render({ page, scale }); const second = b.render({ page, scale }); const width = Math.max(first.width, second.width); const height = Math.max(first.height, second.height); let pixels = 0; let [left, top, right, bottom] = [width, height, -1, -1]; for (let y = 0; y < height; y++) { for (let x = 0; x < width; x++) { if (!differs(first, second, x, y)) continue; pixels++; [left, top] = [Math.min(left, x), Math.min(top, y)]; [right, bottom] = [Math.max(right, x), Math.max(bottom, y)]; } } const box = pixels === 0 ? null : { x: left / scale, y: top / scale, width: (right - left + 1) / scale, height: (bottom - top + 1) / scale }; return { page, pixels, box };}
/** `SCALE`, or less when the page would pass the renderer's pixel or side limit at it. */function fittingScale(doc: RenderDocument, page: number): number { const { width, height } = doc.page(page); const byArea = Math.sqrt(DEFAULT_LIMITS.maxRenderPixels / (width * height)); const bySide = DEFAULT_LIMITS.maxRenderSide / Math.max(width, height); return Math.min(SCALE, byArea, bySide);}
/** Whether the pixel at (x, y) differs; a pixel only one page has always does. */function differs(first: RenderedTile, second: RenderedTile, x: number, y: number): boolean { if (x >= first.width || y >= first.height || x >= second.width || y >= second.height) return true; const i = (y * first.width + x) * 4; const j = (y * second.width + x) * 4; for (let channel = 0; channel < 4; channel++) { const delta = Math.abs((first.pixels[i + channel] ?? 0) - (second.pixels[j + channel] ?? 0)); if (delta > TOLERANCE) return true; } return false;}Each page gets one of two outcomes. With no differing pixels, the file’s structure changed but its pages look the same. With some, the box shows where on the page, in points from the top-left corner. The same renderer draws both files, so its own small differences from other viewers cancel out.
To read the change itself, build with compress: false (with_compress(false) in Rust). The content streams then stay readable text, and a diff tool shows what moved. In Rust, the laminapdf-render crate’s hayro feature draws pages the same way.
Upgrade Lamina PDF
Section titled “Upgrade Lamina PDF”Pin the exact Lamina PDF version, so an upgrade happens only when you choose it. A release that changes the bytes written for the same input lists that change first in its changelog. Upgrade in a change of its own, regenerate the references with update set, and look at the page comparison before you commit them.
Keep fonts and edits repeatable
Section titled “Keep fonts and edits repeatable”Fonts need the same care. The embedded subset depends on the font file’s exact bytes, so keep the font your test uses in your own repository rather than loading it from the system. Edited files repeat too: opening a PDF, applying the same edits and saving writes the same bytes each time.
The stability policy states the promise these tests rely on: the same input gives the same bytes everywhere.