Repository navigation
docs(installation): render PDFs in a Web Worker so autoCsp apps work - #7
Merged
Merged
Conversation
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>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The installation page's "Render a PDF" path breaks in a production Angular browser app. With
security.autoCsp: true, Angular writes a<meta>CSP whosescript-srchas no'wasm-unsafe-eval', so Chromium refuses to compile the engine's WebAssembly. A@formepdf/coreimport in a component kills the app at bootstrap, because the wasm loads with the module.ng servehas no CSP, so this only shows up afterng 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 coversng generate web-worker, theangular.jsonasset entry that copies the.wasm, the worker for each base, a smallrenderInWorkerhelper with oneMessageChannelper call so concurrent renders get their own replies, and a note on fonts. The.mdmirror andllms-full.txtcome from the same file.What I found
@formepdf/core0.25)takumi-pdf0.11)WebAssembly.instantiateStreaming(): ... violates the following Content Security policy directive../pkg/takumi_pdf_wasm_bg.wasm?urlandnode:fs/promises, and esbuild resolves neither@formepdf/core/workerwithinit(new URL("forme/forme_bg.wasm", location.href))takumi-pdf/no-initwithinit({ module_or_path: new URL(...) })?No registered font covers ≥ (U+2265), ...fonts(URL or data URI). Forme subsets it (a 760 KB TTF became a 9.8 KB PDF)fonts: [url]in the render optionsAngular bundles worker files with esbuild and no plugins (
bundleWebWorkerin@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/workerover rawpkg-web/forme.jsbecause itsrenderSerializedDocfetches font URLs. The worker build resolves its@formepdf/reactimport into a lazy chunk thatrenderSerializedDocnever 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 andtsconfig.worker.jsonare app config thatshadcn addcan'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 withng newplusshadcn addfrom this branch's registry..catch(String)to record its expected error. PDF text read back with pdf.js:Invoice ? ? ? ?(Helvetica)Invoice ≥ ≤ → ✓(NotoSans family name, DejaVu file)Error: No registered font covers ≥ (U+2265), ≤ (U+2264), → (U+2192), ✓ (U+2713). Register one that does.Invoice ≥ ≤ → ✓invoice-classiceach got their own 24,320-byte PDF. A Zone.js app (provideZoneChangeDetection+zone.jspolyfill) works with the same worker. The wasm URL relative tolocation.hrefresolves.pnpm verifypasses: check, typecheck, parity, registry build, site build, install check.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