# PDF overview and getting started

> Install Birch UI PDF, open a document in the viewer or headlessly, and save annotations and form values back into the PDF.

Birch UI PDF is a PDF viewer and editor for the browser. It draws the pages with its own rendering engine and adds annotations with comment threads, form filling and a form designer, page tools, true redaction, certificate signatures, password protection, document comparison and its own PDF writer, which saves your changes as an incremental update so the original bytes and existing digital signatures are preserved.

The document logic also works without a DOM through `openPdfDocument`, so the same code can fill forms in a Node service, stamp invoices in a worker or redact files before they are archived.

> **PDF: Paid feature. Fully functional without a key; a license removes the watermark. Pricing: https://docs.birchui.com/getting-started/pricing.md**
>
> Birch UI PDF is a commercial product: $599 per developer, a one-time purchase with free updates forever, unlimited apps and unlimited end users. Without a license key every feature works and a small watermark is shown; on `localhost` the watermark is skipped while you develop.

## Install

```sh
npm install @birchui/core @birchui/license @birchui/pdf
```

There is nothing else to install: no third-party renderer and no peer dependency besides the two Birch UI packages. The rendering engine is loaded with a dynamic `import()` the first time a viewer opens a document. Importing `@birchui/pdf` never touches `window` or `document`, so it is safe in server-side rendering.

Styles are injected into the page when the first viewer is created. Pass `injectStyles: false` and import `@birchui/pdf/styles.css` if you prefer a stylesheet.

## The render worker

Pages are parsed and rasterised in a web worker when the browser has `Worker` and `OffscreenCanvas`. Otherwise the same engine runs on the main thread in short time slices. You do not configure anything for the default:

```ts
import { createPdfViewer } from '@birchui/pdf';

createPdfViewer(container, { document: bytes });
```

The viewer creates its worker with `new Worker(new URL('./render-worker.js', import.meta.url), { type: 'module' })`. Bundlers that understand this pattern (Vite, webpack 5, Rollup with a plugin, Parcel) emit the worker file. When yours does not, pass a function that creates the worker from the `@birchui/pdf/render-worker` entry, or pass `worker: false` to stay on the main thread:

```ts
import { createPdfViewer } from '@birchui/pdf';

const worker = () => new Worker(new URL('@birchui/pdf/render-worker', import.meta.url), { type: 'module' });
createPdfViewer(container, { worker, document: bytes });
```

| `worker` | Effect |
| --- | --- |
| not set | A worker where `Worker` and `OffscreenCanvas` exist, otherwise the main thread |
| `true` | The viewer creates the worker |
| a `Worker`, or a function that returns one | That worker is used |
| `false` | The main thread, with time slicing |

When the worker cannot be started, the viewer renders on the main thread instead. It logs one console warning when the worker was set explicitly. `viewer.getRenderStats().renderMode` is `'worker'` or `'main'`. See [Troubleshooting](https://docs.birchui.com/pdf/troubleshooting.md#the-render-worker-does-not-start).

## Open a document

Given bytes, the package makes no network request. Fetch or read the bytes yourself and pass a `Uint8Array`, an `ArrayBuffer` or a `Blob`. A document given as `{ url }` is loaded only when you pass `allowFetch: true` or a `fetchDocument` callback; a `{ rangeLoader }` source reads through your own function. See [Progressive loading](https://docs.birchui.com/pdf/rendering.md#progressive-loading).

```ts
import { createPdfViewer } from '@birchui/pdf';

const response = await fetch('/contracts/services-agreement-fabrikam-2026.pdf');
const viewer = createPdfViewer(document.querySelector<HTMLElement>('#contract')!, {
  licenseKey: 'LQ1.…',
  document: await response.arrayBuffer(),
  fileName: 'services-agreement-fabrikam-2026.pdf',
  currentUser: { name: 'Priya Natarajan', id: 'u-1042' },
  zoom: 'fit-width',
});

await viewer.ready;
viewer.on('pageChange', ({ pageIndex, pageCount }) => console.info(`page ${pageIndex + 1} of ${pageCount}`));
viewer.on('documentError', ({ message }) => console.error(message));
```

The container needs a height, for example `#contract { height: 100vh }`, or pass `height: 720`. `viewer.ready` resolves when the first page can be shown and rejects when the file cannot be opened. Open another file later with `viewer.load(bytes, { fileName })`.

A password-protected file shows a password prompt. See [Encryption and passwords](https://docs.birchui.com/pdf/encryption.md).

## Save

```ts
const { bytes, mode } = await viewer.save();
await fetch('/contracts/services-agreement-fabrikam-2026.pdf', {
  method: 'PUT',
  body: new Blob([bytes], { type: 'application/pdf' }),
});
```

The cast is for TypeScript 5.7 and later, where a `Uint8Array` is no longer accepted as a `BlobPart` without it. `mode` is `incremental` when the changes could be appended to the original file, and `full` after reordering, deleting, inserting or merging pages, applying redactions or changing the encryption. See [Saving](https://docs.birchui.com/pdf/saving.md).

## Headless editing

```ts
import { readFile, writeFile } from 'node:fs/promises';
import { openPdfDocument } from '@birchui/pdf/engine';

const pdf = openPdfDocument(await readFile('onboarding-form.pdf'), { author: 'HR onboarding service' });
pdf.setFormValues({ 'employee.name': 'Mei Chen', 'employee.start': '01/04/2026', 'policy.accepted': true });
pdf.addAnnotation({ type: 'stamp', pageIndex: 0, rect: [400, 720, 560, 772], stamp: 'Received' });
await writeFile('onboarding-form-mei-chen.pdf', (await pdf.save()).bytes);
```

`openPdfDocument` is synchronous. Use `loadPdfDocument(bytes, { password })`, which returns a promise, for encrypted files. The headless API needs no DOM; saving large streams uses `CompressionStream` and the signature and encryption features use Web Crypto, both of which exist in current browsers, workers and Node 20 or later.

## Web component and frameworks

`createPdfViewer` returns the shared Birch UI widget contract: `element`, `update(partial)`, `on(event, listener)` and `destroy()`. `pdfViewerDefinition` is the widget definition (tag name `birchui-pdf-viewer`, with the event names in `PDF_VIEWER_EVENT_NAMES`) that the adapters accept, in the same way as the definitions of the other Birch UI components. See [Web Components](https://docs.birchui.com/frameworks/web-component.md) for `createElementRegistration`.

## What is in the box

- **Viewer**: virtualized scrolling in three scroll modes, two-page spreads, presentation mode, search, ten sidebar panels, a configurable toolbar, themes and a print dialog. See [Viewer](https://docs.birchui.com/pdf/viewer.md).
- **Annotations**: text markup, notes, text boxes and callouts, shapes, ink with an eraser, measurements, stamps, images, signatures and redaction marks, with comment threads and undo. See [Annotations](https://docs.birchui.com/pdf/annotations.md).
- **Forms**: filling, Acrobat format and calculation scripts, and a form designer. See [Forms](https://docs.birchui.com/pdf/forms.md).
- **Page tools**: reorder, rotate, delete, insert, merge, extract, split and crop. See [Page tools](https://docs.birchui.com/pdf/page-tools.md).
- **Saving**: incremental updates, full rewrites, compression, flattening and optimizing. See [Saving](https://docs.birchui.com/pdf/saving.md).
- **Redaction**: marks that really remove text, images and vector art. See [Redaction](https://docs.birchui.com/pdf/redaction.md).
- **Signatures**: drawn, typed and image signatures, certificate signing and validation. See [Signatures](https://docs.birchui.com/pdf/signatures.md).
- **Encryption**: open, add, change and remove password protection. See [Encryption and passwords](https://docs.birchui.com/pdf/encryption.md).
- **Compare**: word-level comparison of two files with a side-by-side view. See [Comparing documents](https://docs.birchui.com/pdf/compare.md).
- **Document tools**: watermarks, headers and footers, Bates numbers, attachments, bookmarks, metadata, page labels, splitting, images to PDF and an archive check. See [Document tools](https://docs.birchui.com/pdf/document-tools.md).
- **Text**: content editing and supplied fonts for non-Latin text. See [Content editing](https://docs.birchui.com/pdf/content-editing.md) and [Fonts](https://docs.birchui.com/pdf/fonts.md).
- **Reference**: [API and events](https://docs.birchui.com/pdf/api.md), [Keyboard shortcuts](https://docs.birchui.com/pdf/keyboard.md), [Accessibility](https://docs.birchui.com/pdf/accessibility.md), [Performance](https://docs.birchui.com/pdf/performance.md), [Troubleshooting](https://docs.birchui.com/pdf/troubleshooting.md).

Read [Limitations](https://docs.birchui.com/pdf/limitations.md) before you commit to a feature: it lists what the package does not do.
