Skip to content

docs(installation): render PDFs in a Web Worker so autoCsp apps work - #7

Merged
aFlamee merged 1 commit into
developfrom
fix/browser-rendering-strict-csp
Nov 15, 2026
Merged

aFlamee merged 1 commit into
developfrom
fix/browser-rendering-strict-csp

Conversation

@aFlamee

@aFlamee aFlamee commented Nov 15, 2026

Copy link
Copy Markdown
Owner

The installation page's "Render a PDF" path breaks in a production Angular browser app. With security.autoCsp: true, Angular writes a <meta> CSP whose script-src has no 'wasm-unsafe-eval', so Chromium refuses to compile the engine's WebAssembly. A @formepdf/core import in a component kills the app at bootstrap, because the wasm loads with the module. ng serve has no CSP, so this only shows up after ng build.

This adds a "Render in the Browser" section to the installation docs. The app serializes the document as before, and a module Web Worker runs only the engine. The <meta> policy doesn't cover the worker. The section covers ng generate web-worker, the angular.json asset entry that copies the .wasm, the worker for each base, a small renderInWorker helper with one MessageChannel per call so concurrent renders get their own replies, and a note on fonts. The .md mirror and llms-full.txt come from the same file.

What I found

Forme (@formepdf/core 0.25) Takumi (takumi-pdf 0.11)
Documented import in an Angular browser build Builds in a zoneless app. Fails at runtime under autoCsp: WebAssembly.instantiateStreaming(): ... violates the following Content Security policy directive Doesn't build. The default entry imports ../pkg/takumi_pdf_wasm_bg.wasm?url and node:fs/promises, and esbuild resolves neither
Entry that works in Angular's worker bundle @formepdf/core/worker with init(new URL("forme/forme_bg.wasm", location.href)) takumi-pdf/no-init with init({ module_or_path: new URL(...) })
Characters outside WinAnsi (≥ ≤ → ✓) Unregistered families fall back to Helvetica/Times/Courier and print ? Rejects the render: No registered font covers ≥ (U+2265), ...
Fix for those characters Add the font to the document's fonts (URL or data URI). Forme subsets it (a 760 KB TTF became a 9.8 KB PDF) Pass fonts: [url] in the render options

Angular bundles worker files with esbuild and no plugins (bundleWebWorker in @angular/build), so it can't compile @Components in a worker. That's why the serializer stays on the main thread and only the engine moves. I used @formepdf/core/worker over raw pkg-web/forme.js because its renderSerializedDoc fetches font URLs. The worker build resolves its @formepdf/react import into a lazy chunk that renderSerializedDoc never loads.

Docs instead of a shipped helper

I kept this to docs. The worker file has to live in the app, because Angular only detects new Worker(new URL("./x.worker", import.meta.url)) in app source. The asset entry and tsconfig.worker.json are app config that shadcn add can't write. The wasm path is the app's choice. A registry helper would save about ten lines and still leave all three steps to the reader.

Proof

Headless Chromium (Playwright, chromium-1254) against Angular 22.2 production builds with security.autoCsp: true. All scratch apps live outside the repo, made with ng new plus shadcn add from this branch's registry.

  • Repro. The documented Forme render on the main thread logs the CSP WebAssembly error above and the app never renders.
  • Recipe, verbatim. Two fresh apps (Forme, Takumi) built from the section's code blocks, extracted from the markdown by script. Substitutions: the font path points at a local DejaVuSans.ttf, and the Takumi no-font render gets .catch(String) to record its expected error. PDF text read back with pdf.js:
    • Forme, no font: Invoice ? ? ? ? (Helvetica)
    • Forme, with font: Invoice ≥ ≤ → ✓ (NotoSans family name, DejaVu file)
    • Takumi, no font: rejects with Error: No registered font covers ≥ (U+2265), ≤ (U+2264), → (U+2192), ✓ (U+2713). Register one that does.
    • Takumi, with font: Invoice ≥ ≤ → ✓
  • Also checked. Two concurrent Forme renders of invoice-classic each got their own 24,320-byte PDF. A Zone.js app (provideZoneChangeDetection + zone.js polyfill) works with the same worker. The wasm URL relative to location.href resolves.
  • pnpm verify passes: check, typecheck, parity, registry build, site build, install check.
  • I looked at the rendered section on the built site. The tabs and file titles render.

Reviewed with sol-loop: 1 round with gpt-6.1-sol. Sol verdict: approved

Created with Claude Opus 5.5 in Claude Code.

🤖 Generated with Claude Code

Angular's security.autoCsp writes a meta CSP without 'wasm-unsafe-eval',
so both engines fail to compile their WebAssembly on the page. Document
the worker recipe, the angular.json asset entry, and how to register a
font for characters outside the standard PDF fonts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
pdfcn-angular Ready Ready Preview Nov 15, 2026 9:43pm UTC

@aFlamee
aFlamee merged commit 1ffcb99 into develop Nov 15, 2026
3 checks passed
@aFlamee
aFlamee deleted the fix/browser-rendering-strict-csp branch November 15, 2026 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant