PDF overview and getting startedPDF
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.
Install#
npm install @birchui/core @birchui/license @birchui/pdfThere 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:
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:
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.
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.
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.
Save#
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.
Headless editing#
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 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.
- 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.
- Forms: filling, Acrobat format and calculation scripts, and a form designer. See Forms.
- Page tools: reorder, rotate, delete, insert, merge, extract, split and crop. See Page tools.
- Saving: incremental updates, full rewrites, compression, flattening and optimizing. See Saving.
- Redaction: marks that really remove text, images and vector art. See Redaction.
- Signatures: drawn, typed and image signatures, certificate signing and validation. See Signatures.
- Encryption: open, add, change and remove password protection. See Encryption and passwords.
- Compare: word-level comparison of two files with a side-by-side view. See Comparing documents.
- Document tools: watermarks, headers and footers, Bates numbers, attachments, bookmarks, metadata, page labels, splitting, images to PDF and an archive check. See Document tools.
- Text: content editing and supplied fonts for non-Latin text. See Content editing and Fonts.
- Reference: API and events, Keyboard shortcuts, Accessibility, Performance, Troubleshooting.
Read Limitations before you commit to a feature: it lists what the package does not do.