Skip to content

fix(pdf-viewer): bundle legacy pdfjs-dist build to resolve WASM issues#21

Merged
jdmcleod merged 6 commits intomainfrom
fix/pdf-viewer-legacy-build
Feb 27, 2026
Merged

fix(pdf-viewer): bundle legacy pdfjs-dist build to resolve WASM issues#21
jdmcleod merged 6 commits intomainfrom
fix/pdf-viewer-legacy-build

Conversation

@jdmcleod
Copy link
Collaborator

@jdmcleod jdmcleod commented Feb 25, 2026

WHY

Some PDFs on C12 were throwing errors and not loading certain pages. This seems to be because the WASM distribution was not being found and was not loading in. React pdf got around this by staying pinned to an older version of PDF.js but I think this is a better fix. Bundle the extra stuff with the web component.

Problem

pdfjs-dist v5 requires WebAssembly binaries (jbig2.wasm, openjpeg.wasm, qcms_bg.wasm) at runtime. The initial approach defaulted to loading these from unpkg.com with no integrity verification, creating a supply-chain attack vector where a compromised CDN could deliver arbitrary WASM executed in the user's browser.

Changes

  • Bundle WASM locally — imports all three WASM files via Vite ?url so they are emitted into dist/ at build time. A LocalWasmFactory serves them to PDF.js on demand; no external network requests are made.
  • Bundle legacy pdfjs-dist — Vite aliases redirect pdfjs-dist imports to the legacy build variant (same v5, transpiled with polyfills), resolving compatibility issues with strict CSPs and older bundlers.
  • No consumer configuration required — WASM and the worker are fully bundled; consumers get a working PDF viewer out of the box.

jdmcleod and others added 5 commits February 25, 2026 14:39
Bundles the legacy build of pdfjs-dist which includes JS fallbacks for environments with strict WASM policies or lacking top-level await support. This resolves compatibility issues with consumers using older bundlers or strict CSPs.
When pdfjs-dist runs its worker as a data URI, WASM files cannot be found via relative paths (import.meta.url resolves to the data URI). Providing wasmUrl to getDocument() gives the worker an absolute URL to fetch WASM from. Defaults to unpkg.com using the bundled pdfjs version so no consumer configuration is needed. Consumers can override with the wasm-url attribute if needed (e.g. for offline environments or self-hosted WASM).
… default

Replace the DEFAULT_WASM_URL (unpkg.com CDN) with a LocalWasmFactory that
uses ?url imports to bundle jbig2.wasm, openjpeg.wasm, and qcms_bg.wasm
directly into the dist output. This eliminates the supply-chain risk of
loading unverified WASM from an external CDN.

- wasmUrl now defaults to null (bundled WASM used automatically)
- loadPDF() uses WasmFactory: LocalWasmFactory by default; falls back to
  the URL-based approach only when wasm-url is explicitly set by a consumer
- Document wasm-url in the README attributes table with a security note

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…M loading

Remove the wasm-url attribute/property entirely. LocalWasmFactory is
sufficient for all use cases — there is no need for a consumer-facing URL
override that would allow arbitrary WASM to be loaded without integrity
checks.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jdmcleod jdmcleod merged commit 4782c33 into main Feb 27, 2026
1 check passed
@jdmcleod jdmcleod deleted the fix/pdf-viewer-legacy-build branch February 27, 2026 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants