Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 82 additions & 26 deletions apps/web/content/docs/(root)/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ addEventListener("message", async ({ data, ports: [port] }) => {
}
```

The worker entry also imports `@formepdf/react`, which only Forme's React API uses. Add it to `externalDependencies` in the same build options, so `ng build` leaves it out of the bundle:

```json title="angular.json"
"externalDependencies": ["@formepdf/react"]
```

```ts title="src/app/pdf.worker.ts"
/// <reference lib="webworker" />
import { init, renderSerializedDoc } from "@formepdf/core/worker";
Expand All @@ -197,65 +203,96 @@ addEventListener("message", async ({ data, ports: [port] }) => {
:::
::::

In your app, send each document to the worker with its own `MessageChannel`, so that concurrent renders get their own replies:
In your app, start the worker and load the document on the first render. The engine and its `.wasm` file then download only when someone asks for a PDF. Send each document 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",
});
```ts title="src/app/pdf.ts"
let worker: Worker | undefined;

const renderInWorker = (message: unknown) =>
new Promise<Uint8Array>((resolve, reject) => {
worker ??= new Worker(new URL("./pdf.worker", import.meta.url), {
type: "module",
});
const current = worker;
// A worker that fails to load never replies, so reject instead of waiting.
const onError = (event: Event) => {
if (worker === current) {
worker = undefined;
}
reject(new Error("The PDF worker failed", { cause: event }));
};
current.addEventListener("error", onError);
const { port1, port2 } = new MessageChannel();
port1.onmessage = ({ data }) =>
port1.onmessage = ({ data }) => {
current.removeEventListener("error", onError);
data.error ? reject(data.error) : resolve(data.pdf);
worker.postMessage(message, [port2]);
};
current.postMessage(message, [port2]);
});

const { node } = renderTakumiDocument(Invoice);
const pdf = await renderInWorker({ node, options: { size: "a4" } });
export const renderInvoice = async () => {
const [{ renderTakumiDocument }, { Invoice }] = await Promise.all([
import("@/lib/render"),
import("./invoice"),
]);
const { node } = renderTakumiDocument(Invoice);
return 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",
});
```ts title="src/app/pdf.ts"
let worker: Worker | undefined;

const renderInWorker = (message: unknown) =>
new Promise<Uint8Array>((resolve, reject) => {
worker ??= new Worker(new URL("./pdf.worker", import.meta.url), {
Comment thread
aFlamee marked this conversation as resolved.
Resolved
type: "module",
});
const current = worker;
// A worker that fails to load never replies, so reject instead of waiting.
const onError = (event: Event) => {
if (worker === current) {
worker = undefined;
}
reject(new Error("The PDF worker failed", { cause: event }));
};
current.addEventListener("error", onError);
const { port1, port2 } = new MessageChannel();
port1.onmessage = ({ data }) =>
port1.onmessage = ({ data }) => {
current.removeEventListener("error", onError);
data.error ? reject(data.error) : resolve(data.pdf);
worker.postMessage(message, [port2]);
};
current.postMessage(message, [port2]);
});

const pdf = await renderInWorker(renderFormeDocument(Invoice));
export const renderInvoice = async () => {
const [{ renderFormeDocument }, { Invoice }] = await Promise.all([
import("@/lib/render"),
import("./invoice"),
]);
return 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:
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. In `renderInvoice`, replace the lines after the imports:

::::tabs
:::tab{value="takumi" label="Takumi"}

```ts
const pdf = await renderInWorker({
const { node } = renderTakumiDocument(Invoice);
return renderInWorker({
node,
options: { size: "a4", fonts: ["/fonts/NotoSans-Regular.ttf"] },
});
Expand All @@ -264,6 +301,25 @@ const pdf = await renderInWorker({
:::
:::tab{value="forme" label="Forme"}

Name the font in your theme's typography. Add the theme at the top of `src/app/pdf.ts`:

```ts title="src/app/pdf.ts"
import { professionalTheme } from "@/lib/pdf-themes/professional";

const theme = {
...professionalTheme,
typography: {
body: { ...professionalTheme.typography.body, fontFamily: "Noto Sans" },
heading: {
...professionalTheme.typography.heading,
fontFamily: "Noto Sans",
},
},
};
```

Pass it to the document and register the font:

```ts
const doc = renderFormeDocument(Invoice, { inputs: { theme } });
doc.fonts = [
Expand All @@ -275,10 +331,10 @@ doc.fonts = [
italic: false,
},
];
const pdf = await renderInWorker(doc);
return 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.
`Invoice` needs a `theme` input that it binds to `<pdf-theme-provider [theme]="theme()">`, as the blocks do. `family` must match the font family that the theme names. `src` takes a URL or a data URI. The worker fetches the file when it renders, so the font doesn't load with your app. Forme embeds only the glyphs that the document uses.

:::
::::
Expand Down
6 changes: 4 additions & 2 deletions apps/web/content/docs/components/forme/keep-together.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "Keep Together"
description: "Keeps children on the same PDF page."
description: "Keeps children on one PDF page when they fit."
---

::preview{base="forme" name="keep-together"}
Expand All @@ -22,4 +22,6 @@ description: "Keeps children on the same PDF page."
| `minPresenceAhead` | `number` | - |
| `style` | `Style` | - |

The content to keep on one page is projected content.
The content to keep on one page is projected content. If it doesn't fit in the space left on the page, it starts on the next page. Text taller than a full page still splits across pages, so it leaves the rest of the previous page empty. Let long text flow instead of wrapping it.

Forme ignores `minPresenceAhead`. The input exists for parity with pdfcn.
6 changes: 3 additions & 3 deletions apps/web/lib/content/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { existsSync, readFileSync } from "node:fs";
import path from "node:path";

import { REGISTRY_NAMESPACE } from "../install";
import { LICENSE_NOTICE, REGISTRY_NAMESPACE } from "../install";
import { APP_ROOT } from "./source";

interface RegistryItem {
Expand Down Expand Up @@ -70,10 +70,10 @@ export const installPlan = (itemName: string, docFile: string) => {
]
.filter((file) => existsSync(path.join(APP_ROOT, file.path)))
.map((file) => ({
code: toInstalledPaths(
code: `${toInstalledPaths(
readFileSync(path.join(APP_ROOT, file.path), "utf-8"),
targets
).trim(),
).trim()}\n\n${LICENSE_NOTICE}`,
target: file.target,
}));
return { files, packages };
Expand Down
4 changes: 4 additions & 0 deletions apps/web/lib/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ export const TSCONFIG_PATHS = { "@/*": ["./src/*"] };

export const addCommand = (...names: string[]) =>
`npx shadcn@latest add ${names.map((name) => `${REGISTRY_NAMESPACE}/${name}`).join(" ")}`;

/** The attribution line at the end of every installed file. `shadcn add` strips leading comments. */
export const LICENSE_NOTICE =
"// Copyright (c) 2026 Shadcn Labs and Ahmad El Mir. MIT License: https://github.com/aFlamee/angular-pdfcn/blob/main/LICENSE";
4 changes: 2 additions & 2 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"@fontsource-variable/geist": "^5.3.0",
"@fontsource-variable/geist-mono": "^5.3.0",
"@fontsource-variable/inter": "^5.3.0",
"@formepdf/core": "^0.25.0",
"@formepdf/shared": "^0.25.0",
"@formepdf/core": "^0.32.0",
"@formepdf/shared": "^0.32.0",
"@takumi-rs/helpers": "^2.10.0",
"@vercel/analytics": "^2.0.1",
"@web-kits/audio": "^0.1.0",
Expand Down
6 changes: 1 addition & 5 deletions apps/web/public/r/forme/alert.json

Large diffs are not rendered by default.

Loading
Loading