Typed TypeScript access to the PDFium WASM worker used by the pdfrx_web viewer packages. Use it directly for PDF rendering, text and link extraction, forms, annotations, page editing, or PDF encoding without a viewer UI. It supports browsers, Node.js, Bun, and Deno.
npm package · API reference · Detailed guide
npm install @pdfrx/engine
import { PdfrxEngine } from '@pdfrx/engine';
const engine = new PdfrxEngine({
wasmModulesUrl: 'https://cdn.jsdelivr.net/npm/@pdfrx/engine@0.27.0/assets/',
});
const document = await engine.openUrl('/manual.pdf');
const page = document.pages[0];
if (page) {
const image = await page.render({
fullWidth: page.width * 2,
fullHeight: page.height * 2,
});
if (image) canvasContext.putImageData(image.toImageData(), 0, 0);
}
await document.dispose();
engine.dispose();
In browsers, wasmModulesUrl must point to a directory containing
pdfium_worker.js and pdfium.wasm. Copy both from
node_modules/@pdfrx/engine/assets/ or serve the versioned CDN directory shown
above. Non-browser runtimes discover the installed assets automatically.
PdfrxEngine
owns the worker and opens
PdfDocument
instances:
openUrl()
opens a remote PDF, optionally using HTTP range access and a password
callback.openData()
opens bytes supplied as an ArrayBuffer, typed array, or compatible binary
source.The same
PdfrxEngine
creates new
PdfDocument
instances, then creates and arranges their pages:
createNew()
creates an empty document.createPagesFromImages()
creates one unplaced PDF page per supplied image; arrange the returned pages
with setPages().createPagesFromContents()
creates complete, unplaced pages on a document from declarative
PdfPageContentSpec
values containing text, image, and vector-path objects. Arrange the returned
pages with setPages().
See the page-content guide below.Documents expose their current page arrangement through
PdfDocument.pages.
Each
PdfPage
provides:
render()
for full-page or partial-region RGBA rendering.loadText()
for text plus per-character PDF-coordinate rectangles.loadLinks()
and
loadAnnotations()
for interactive page content.At document scope,
loadOutline()
reads bookmarks and
loadFormFields()
reads AcroForm controls and their values.
setPages()
and
setPage()
arrange, import, duplicate, remove, or rotate pages without immediately
rewriting the physical page tree.addAnnotation(),
updateAnnotation(),
and
removeAnnotation()
modify page annotations.prepareFreeTextAppearance()
prepares wrapping, mixed-script font runs, and emoji images before a
FreeText annotation is added or updated. Its
PdfTextAppearanceServices
can replace measurement, font resolution, or emoji rendering.setFormFieldValue()
and
setFormFieldValues()
update form controls and run supported calculations.setOutline()
replaces the logical bookmark tree.Document mutation events report page, annotation, and form changes with origin, transaction, and actor metadata, allowing applications to build persistence, history, or synchronization without polling.
Server runtimes can explicitly opt into local filesystem fonts:
const engine = new PdfrxEngine({
localFonts: {
systemDirectories: true,
directories: ['./fonts'],
},
fontCache: {
directory: './.cache/pdfrx-fonts',
persistRegisteredFonts: true,
},
});
PdfrxLocalFontsOptions
controls OS and application font directories. The engine indexes internal font
names and lazily loads missing faces.
PdfrxFontCacheOptions
caches that index and can optionally persist bytes passed to addFontData().
Persisting font bytes is disabled by default so the application can enforce its
font licenses and data-handling policy. Deno requires matching filesystem
permissions.
encodePdf()
returns PDF bytes. Its copy mode materializes a temporary document so the live
document and page proxies remain usable; compact mode rebuilds reachable
page-level content with lower retention of document-level structures. The
default live-document materialization is an explicit state boundary, so consult
the guide before combining it with application Undo/Redo.
Dispose every document with
PdfDocument.dispose()
and dispose the engine when finished. In Node.js, the engine worker otherwise
keeps the process alive.
PdfrxEngine API
lists all open/create methods and engine options.@pdfrx/engine ·
@pdfrx/viewer ·
@pdfrx/reactMIT
@pdfrx/engine— a typed client for rendering PDF documents.The heavy work runs off the main thread in a dedicated worker (a WASM rendering engine); this package speaks a
postMessagecommand protocol to that worker and exposes an idiomatic,Promise-based object model on top of it. The worker is started the way the host runs workers — a Web Worker in a browser, the platform equivalent on Node, Bun and Deno — so the same package works on a server runtime without extra configuration.The primary entry point is PdfrxEngine: construct one with the URL of the directory that hosts the bundled WASM assets (
pdfium_worker.jsandpdfium.wasm), then open documents with PdfrxEngine.openUrl, PdfrxEngine.openData, PdfrxEngine.createNew, or PdfDocument.createPagesFromImages. Opened documents are represented by PdfDocument, their pages by PdfPage, and rendered bitmaps by PdfImage.