---
title: "Show PDFs, on one page"
description: "Every page of the docs' Show PDFs section, in order, on one page."
version: "0.9.0"
url: "https://docs.laminapdf.com/one-page/show-pdfs/"
---

# Show PDFs, on one page

Every page of the docs' Show PDFs section, in order, on one page.

## Show a PDF in a web page

Put a scrolling, zoomable PDF viewer in your page with @laminapdf/viewer, drawn by PDFium in a Web Worker.

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.

> **Preview:** `@laminapdf/viewer` is a preview. It is the core of [the Lamina PDF reader](https://app.laminapdf.com), and its API can still change between releases while the reader grows.

Install the viewer and the PDFium build it draws with:

```sh
npm install @laminapdf/viewer @embedpdf/pdfium@2.15.1
```

This example shows a file the user picked, in a container on your page:

`packages/viewer/examples/show-a-pdf.ts`:

```ts
// 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:

```ts
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](#take-it-down) explains, call its `close()` when you're done.

### 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

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

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.

### Next

[How a page is drawn](/concepts/drawing-pages.md) 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](/guides/render-pages.md).

## Render pages to images

Draw a PDF's pages as pixels, for thumbnails, previews or tests, in JavaScript and from the command line.

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

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:

`packages/sdk/examples/draw-page.ts`:

```ts
// 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](/concepts/drawing-pages.md) explains the tile plan.

### From the command line

`laminapdf render` writes pages as PNG files, which suits scripts and batch jobs:

```console
$ 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.png
page 2: 419 × 595 px → thumbs/report-002.png
page 3: 419 × 595 px → thumbs/report-003.png
```

[Extract text and render pages](/cli/extract-and-render.md#images-render) lists its options.

### 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](https://docs.rs/laminapdf-render). 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](/guides/test-generated-pdfs.md), which draws two PDFs and counts the pixels that differ.
