diff --git a/apps/web/content/docs/(root)/installation.md b/apps/web/content/docs/(root)/installation.md index 039a7a0..5da2e74 100644 --- a/apps/web/content/docs/(root)/installation.md +++ b/apps/web/content/docs/(root)/installation.md @@ -122,7 +122,166 @@ const pdf = await renderPdf(JSON.stringify(renderFormeDocument(Invoice))); ::: :::: -`pdf` holds the PDF bytes as a `Uint8Array`. To set the component's inputs, pass them as `inputs`, for example `renderTakumiDocument(InvoiceClassicDocument, { inputs: { theme } })`. +`pdf` holds the PDF bytes as a `Uint8Array`. To set the component's inputs, pass them as `inputs`, for example `renderTakumiDocument(InvoiceClassicDocument, { inputs: { theme } })`. In a browser app, render in a Web Worker instead, as the next section shows. + +## Render in the Browser + +Both engines compile WebAssembly, and a strict Content Security Policy blocks that on the page. Angular's `security.autoCsp` option writes such a policy as a `` tag. It can allow WebAssembly only through `unsafeEval`, which also allows `eval()`. A `` policy doesn't apply to Web Workers, so run the engine in a worker. Your app serializes the document, and the worker turns it into PDF bytes. + +Angular bundles workers without plugins, so the worker loads each engine's web build and fetches the `.wasm` file itself. Create the worker: + +```bash +ng generate web-worker pdf +``` + +Copy the engine's `.wasm` file into the build output with an entry in the `assets` array of `angular.json`, then replace the generated worker: + +::::tabs +:::tab{value="takumi" label="Takumi"} + +```json title="angular.json" +{ + "glob": "takumi_pdf_wasm_bg.wasm", + "input": "node_modules/takumi-pdf/pkg", + "output": "takumi" +} +``` + +```ts title="src/app/pdf.worker.ts" +/// +import init, { render } from "takumi-pdf/no-init"; + +const ready = init({ + module_or_path: new URL("takumi/takumi_pdf_wasm_bg.wasm", location.href), +}); + +addEventListener("message", async ({ data, ports: [port] }) => { + try { + await ready; + const pdf = await render(data.node, data.options); + port.postMessage({ pdf }, [pdf.buffer]); + } catch (error) { + port.postMessage({ error }); + } +}); +``` + +::: +:::tab{value="forme" label="Forme"} + +```json title="angular.json" +{ + "glob": "forme_bg.wasm", + "input": "node_modules/@formepdf/core/pkg-web", + "output": "forme" +} +``` + +```ts title="src/app/pdf.worker.ts" +/// +import { init, renderSerializedDoc } from "@formepdf/core/worker"; + +const ready = init(new URL("forme/forme_bg.wasm", location.href)); + +addEventListener("message", async ({ data, ports: [port] }) => { + try { + await ready; + const pdf = await renderSerializedDoc(data); + port.postMessage({ pdf }, [pdf.buffer]); + } catch (error) { + port.postMessage({ error }); + } +}); +``` + +::: +:::: + +In your app, send each document to the worker with its own `MessageChannel`, so that concurrent renders get their own replies: + +::::tabs +:::tab{value="takumi" label="Takumi"} + +```ts +import { renderTakumiDocument } from "@/lib/render"; +import { Invoice } from "./invoice"; + +const worker = new Worker(new URL("./pdf.worker", import.meta.url), { + type: "module", +}); + +const renderInWorker = (message: unknown) => + new Promise((resolve, reject) => { + const { port1, port2 } = new MessageChannel(); + port1.onmessage = ({ data }) => + data.error ? reject(data.error) : resolve(data.pdf); + worker.postMessage(message, [port2]); + }); + +const { node } = renderTakumiDocument(Invoice); +const pdf = await renderInWorker({ node, options: { size: "a4" } }); +``` + +::: +:::tab{value="forme" label="Forme"} + +```ts +import { renderFormeDocument } from "@/lib/render"; +import { Invoice } from "./invoice"; + +const worker = new Worker(new URL("./pdf.worker", import.meta.url), { + type: "module", +}); + +const renderInWorker = (message: unknown) => + new Promise((resolve, reject) => { + const { port1, port2 } = new MessageChannel(); + port1.onmessage = ({ data }) => + data.error ? reject(data.error) : resolve(data.pdf); + worker.postMessage(message, [port2]); + }); + +const pdf = await renderInWorker(renderFormeDocument(Invoice)); +``` + +::: +:::: + +### Fonts + +Forme renders every font family you don't register with a standard PDF font: Helvetica, Times-Roman, or Courier. These cover Western European text such as ä, ß, „“, and €, and print other characters, such as ≥, →, and ✓, as `?`. Takumi rejects the render instead, with an error such as `No registered font covers ≥ (U+2265)`. Register a TrueType font that covers your text: + +::::tabs +:::tab{value="takumi" label="Takumi"} + +```ts +const pdf = await renderInWorker({ + node, + options: { size: "a4", fonts: ["/fonts/NotoSans-Regular.ttf"] }, +}); +``` + +::: +:::tab{value="forme" label="Forme"} + +```ts +const doc = renderFormeDocument(Invoice, { inputs: { theme } }); +doc.fonts = [ + ...(doc.fonts ?? []), + { + family: "Noto Sans", + src: "/fonts/NotoSans-Regular.ttf", + weight: 400, + italic: false, + }, +]; +const pdf = await renderInWorker(doc); +``` + +For Forme, `family` must match the font family that your theme's typography names. `src` takes a URL or a data URI. Forme embeds only the glyphs that the document uses. + +::: +:::: ## Install a Block