Show PDFs, on one page
This page shows every Show PDFs page in order. Read offline lists every section and the whole guide as one PDF.
Show a PDF in a web page
You’ll put a PDF on your page that people can scroll and zoom, with text they can select and links within the document they can follow. The viewer draws only the pages near the scroll position, so a 1,000-page file scrolls as smoothly as a short one.
Install the viewer and the PDFium build it draws with:
npm install @laminapdf/viewer @embedpdf/pdfium@2.15.1This example shows a file the user picked, in a container on your page:
// Shows a PDF in a web page. The viewer keeps only the PDF's pages near the scroll position on// screen and draws them with PDFium, in a Web Worker so the web page stays responsive. Pass the// address your bundler serves PDFium's WebAssembly at; with Vite://// import pdfiumWasm from "@embedpdf/pdfium/pdfium.wasm?url";//// The viewer is a preview: its API can still change between releases.
import { createViewer, ViewerClient } from "@laminapdf/viewer";
// Some Safari releases have no Symbol.dispose. The code TypeScript and esbuild compile from `using`// then looks for Symbol.for("Symbol.dispose"), so the shown PDF answers to that key there.const dispose: typeof Symbol.dispose = (Symbol.dispose ?? Symbol.for("Symbol.dispose")) as typeof Symbol.dispose;
/** A PDF on screen. Close it, or declare it with `using`, to take it down and end its Web Worker. */export interface ShownPdf extends Disposable { readonly pageCount: number; /** Takes the PDF down and ends its Web Worker; closing it again does nothing. */ close(): void;}
/** Shows `file` in `container`, which must have a height, drawn by PDFium from `pdfiumWasm`. */export async function showPdf(container: HTMLElement, file: Blob, pdfiumWasm: string): Promise<ShownPdf> { // The client starts the document's Web Worker and loads PDFium into it. const client = await ViewerClient.create({ backend: { backend: "pdfium", wasm: pdfiumWasm } }); const viewer = createViewer({ client }); const close = () => { viewer.destroy(); client.terminate(); }; try { viewer.mount(container); // The file goes to the Web Worker by reference; the web page never reads its bytes. const opened = await client.open(file); viewer.open(opened); return { pageCount: opened.pages.length, close, [dispose]: close }; } catch (error) { // A file that won't open, a damaged one or a locked one, still ends the Web Worker. close(); throw error; }}Call it with an element that has a height, the file from an <input type="file"> or a drop, and the address your bundler serves PDFium’s WebAssembly at:
using shown = await showPdf(container, file, pdfiumWasm);using takes the PDF down when the block it’s declared in ends. When the viewer should stay on the page longer, keep what showPdf returns and, as Take it down explains, call its close() when you’re done.
What runs where
Section titled “What runs where”The viewer splits the work between your page and a Web Worker, one per document:
- The Web Worker opens the file with Lamina PDF and draws pages with PDFium. A slow PDF page never blocks scrolling or typing on your site.
- Your web page holds the view: a shell for each page near the scroll position, the canvases they paint into, and the text and link layers over them.
The file stays in the Web Worker. A File goes to the Web Worker by reference, so your web page never reads its bytes. To show a file from a server instead, pass client.open the file’s address as { url } where the example passes file. The Web Worker then reads it with HTTP range requests, fetching only the parts the visible pages need.
Give the container a size
Section titled “Give the container a size”The viewer fills its container and scrolls inside it, so the container needs a height: a fixed one, or flex: 1 in a column that fills the window. A container with no height shows nothing.
Take it down
Section titled “Take it down”When the PDF leaves the page, call close() on what showPdf returns, or let a using declaration close it at the end of its block. Closing stops the PDF’s renders, frees its canvases and ends its Web Worker; a second close() does nothing. If the file won’t open, because it’s damaged or locked with a password, showPdf ends its Web Worker before it throws.
using works in Safari too. The code TypeScript and esbuild compile from using looks for Symbol.dispose, or for Symbol.for("Symbol.dispose") where the browser has none, and showPdf puts close() under the key that code looks for.
How a page is drawn explains why a renderer draws the pixels while Lamina PDF reads the text. To draw pages to images instead of a scrolling view, see render pages to images.
Render pages to images
You’ll draw a page as an image: a thumbnail for a file list, a preview in an email, or a picture to compare in a test. A renderer turns the page into straight RGBA pixels, and Lamina PDF reads the same page’s text, so you can show both.
In JavaScript
Section titled “In JavaScript”Two renderers implement the same interface, RenderBackend from @laminapdf/render:
| Package | Renderer | Best for |
|---|---|---|
@laminapdf/render-hayro |
hayro, written in Rust and built to WebAssembly | servers, Cloudflare Workers and tests: small, and it needs no address |
@laminapdf/render-pdfium |
PDFium, the engine of Chrome’s PDF viewer | browsers, where pages should look as they do in Chrome |
This example draws a page with hayro and reads its text with Lamina PDF:
// Draws one page to pixels and reads the same page's text, to show who does which job: a renderer// turns the page into RGBA pixels, and Lamina PDF reads the text from its own model of the file.// Both read the same bytes. In Node, load the hayro renderer with the module's bytes://// const wasm = fileURLToPath(import.meta.resolve("@laminapdf/wasm/lamina_pdf_render_hayro.wasm"));// const renderer = await initHayro(readFileSync(wasm));//// Text needs Lamina PDF's text or extract module: `await init({ text: true })`.
import { bytesSource, type RenderBackend } from "@laminapdf/render";import { Document } from "@laminapdf/sdk";
/** Pixels per point: 2 draws at 144 dpi, sharp on most screens. */const SCALE = 2;
/** One page as pixels, with its text beside them. */export interface DrawnPage { readonly width: number; readonly height: number; /** Straight RGBA, four bytes a pixel, row by row from the top-left corner. */ readonly pixels: Uint8ClampedArray; readonly text: string;}
/** Draws page `index` of `bytes` with `renderer` and reads its text with Lamina PDF. */export function drawPage(renderer: RenderBackend, bytes: Uint8Array, index: number): DrawnPage { // The renderer draws the page and nothing else: it never supplies text, links or selection. using drawing = renderer.open(bytesSource(bytes)); const { width, height, pixels } = drawing.render({ page: index, scale: SCALE }); // The text comes from Lamina PDF, which also edits and saves the file, so what you select is what // an edit changes. using doc = Document.open(bytes); const text = doc.page(index).text().text(); return { width, height, pixels, text };}To draw with PDFium instead, start it with initPdfium(wasm) from @laminapdf/render-pdfium, which takes PDFium’s WebAssembly as initHayro takes hayro’s. Then pass the backend it returns to drawPage; nothing else changes.
A scale of 2 draws 144 pixels per inch. Ask for a clip to draw only part of a page. The renderer refuses a tile wider or taller than 16,384 pixels, or larger than about 16.7 million pixels, with render-too-large, so draw a poster-sized page at a high scale in tiles. How a page is drawn explains the tile plan.
From the command line
Section titled “From the command line”laminapdf render writes pages as PNG files, which suits scripts and batch jobs:
$ mkdir -p thumbs$ laminapdf render report.pdf --pages 1-3 --dpi 72 -o 'thumbs/{stem}-{n:03}.png'page 1: 419 × 595 px → thumbs/report-001.pngpage 2: 419 × 595 px → thumbs/report-002.pngpage 3: 419 × 595 px → thumbs/report-003.pngExtract text and render pages lists its options.
In Rust
Section titled “In Rust”The Rust renderers are the laminapdf-render crate, with hayro behind its hayro feature, and laminapdf-render-pdfium for a native PDFium. Their API is at docs.rs. The laminapdf crate doesn’t draw pages yet, so this guide has no Rust example; the command line above runs the same renderers.
Next, see how to compare pages in tests, which draws two PDFs and counts the pixels that differ.