PDF.js configuration
Self-host the PDF.js worker, fonts and decoders for production, choose a build for older browsers, and tune PDF.js per viewer.
PDF.js runs its parser in a Web Worker and loads extra assets on demand: CMaps for CJK text, standard font data for documents that don’t embed their fonts, WebAssembly image decoders and ICC color profiles. The viewer loads PDF.js lazily, the first time a document opens.
Basics
Call configurePdfJs once at startup to self-host the worker and assets.
import { configurePdfJs } from '@kingtech/react-pdf-viewer';
import workerSrc from 'pdfjs-dist/build/pdf.worker.min.mjs?url';
configurePdfJs({
workerSrc,
cMapUrl: '/pdfjs/cmaps/',
standardFontDataUrl: '/pdfjs/standard_fonts/',
wasmUrl: '/pdfjs/wasm/',
iccUrl: '/pdfjs/iccs/',
});
Without configuration everything still works: the worker and assets load from jsDelivr, pinned to the exact installed pdfjs-dist version, and a one-time notice in development reminds you to self-host. Copy the asset folders from node_modules/pdfjs-dist/ as part of your build; the Vite, Next.js and webpack pages show how.
Older browsers
PDF.js 6 relies on recent browser APIs. Load the legacy build for older browsers:
configurePdfJs({
loader: () => import('pdfjs-dist/legacy/build/pdf.mjs'),
workerSrc: legacyWorkerUrl, // from pdfjs-dist/legacy/build/pdf.worker.min.mjs
});
Your own worker
Pass a ready Worker as workerPort when you create the worker yourself, for example to share it or to satisfy a strict Content Security Policy. A worker you have already set on PDF.js’s GlobalWorkerOptions is respected too.
Content Security Policy
worker-srcmust allow the worker’s origin, orblob:when you pass aworkerPortcreated from a blob.connect-srcmust allow the asset URLs (and the document URLs).script-srcneeds no'unsafe-eval': the viewer passesisEvalSupported: false. Documents with JPEG 2000 or JBIG2 images use the WebAssembly decoders, which a strict policy allows with'wasm-unsafe-eval'.
Customization
pdfjsOptions passes per-viewer options to PDF.js getDocument, for example its log level.
Source-related options (URL, data, password, headers) are managed by the viewer and cannot be overridden this way. See the PdfJsDocumentOptions reference and the customization guide.
Advanced / escape hatch
loader can return any pdfjs-dist module instance, for example one your app already imported. getDocument() on the imperative API exposes the loaded PDF.js document for anything the viewer does not wrap.
Limitations
- Configuration is global.
configurePdfJsapplies to every viewer on the page. Workaround: usepdfjsOptionsfor per-viewer differences. - Configure before the first document opens. Changing
loaderlater only affects documents opened afterwards; a worker change applies to the loaded module. Workaround: call it in your entry module. - The CDN default is for development. Loading the worker from a third-party CDN depends on its availability and your CSP. Workaround: self-host as shown above.
- Only the tested
pdfjs-dist6 line is supported. The peer range is^6.0.0; the test suite runs against 6.3. See Requirements.
API
configurePdfJs— Configures PDF.js for every viewer instance. Call it once at startup, before the first viewer mounts. Calls merge with previous configuration.PdfJsConfig— Global PDF.js configuration shared by every viewer: how the module is loaded, the worker, and the URLs of its assets.PdfJsDocumentOptions— Per-instance options forwarded to pdfjs-dist's getDocument (for example cMapUrl, standardFontDataUrl or verbosity). Source-related options are managed by the viewer.PdfJsModule— The pdfjs-dist module namespace, as returned by the loader of configurePdfJs to supply a specific build.