Skip to content
Open
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
37 changes: 37 additions & 0 deletions docs/content/docs/features/export/docx.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,40 @@ const defaultOptions = {
colors: COLORS_DEFAULT, // defaults from @blocknote/core
};
```

### Fonts

DOCX embeds Inter 18pt for body text and Geist Mono for code by default.
Set `fontFamily` or `monoFontFamily` to change the styles. To use a custom
family on devices where it isn't installed, embed it with `fonts`:

```typescript
import { DOCXExporter, docxDefaultSchemaMappings } from "@blocknote/xl-docx-exporter";

const response = await fetch("/fonts/Lato-Regular.ttf");
if (!response.ok) throw new Error("Could not load Lato");

const exporter = new DOCXExporter(editor.schema, docxDefaultSchemaMappings, {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
fontFamily: "Lato",
fonts: [{ name: "Lato", regular: await response.blob() }],
});
```

Use static TrueType files that permit embedding, with one entry per family
and the name declared in the files. Each face accepts a `Blob` (including
`File`), `ArrayBuffer`, or `Uint8Array`. Add `bold`, `italic`, and
`boldItalic` when available. A supplied list replaces all bundled faces,
including the code font; use `fonts: []` to embed none.

To use a font installed on the reader's machine without embedding it, set
its family name (for example, `fontFamily: "Marianne"`). For WOFF or WOFF2,
[unpack it once at build time](/docs/features/export/pdf#unpacking-webfonts)
and pass the resulting static `.ttf` file to `fonts`. If the result is
CFF/OpenType, use the installed family or obtain a static TrueType version.

An unreadable file or unsupported format rejects the export with a
`FontEmbeddingError`. Use the family and face in the error to identify
which file to replace before retrying.

If you supply `documentOptions.fonts` or `documentOptions.externalStyles`
when exporting, those override the corresponding generated values.
36 changes: 36 additions & 0 deletions docs/content/docs/features/export/odt.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,39 @@ const defaultOptions = {
colors: COLORS_DEFAULT, // defaults from @blocknote/core
};
```

### Fonts

ODT embeds Inter 18pt for body text and Geist Mono for code by default.
Set `fontFamily` or `monoFontFamily` to change the default body or code font.
The ODT template keeps its script-specific fonts for East Asian and complex
scripts, such as Arabic. To use a custom
family on devices where it isn't installed, embed it with `fonts`:

```typescript
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
import { ODTExporter, odtDefaultSchemaMappings } from "@blocknote/xl-odt-exporter";

const response = await fetch("/fonts/Lato-Regular.ttf");
if (!response.ok) throw new Error("Could not load Lato");

const exporter = new ODTExporter(editor.schema, odtDefaultSchemaMappings, {
fontFamily: "Lato",
fonts: [{ name: "Lato", regular: await response.blob() }],
});
```

Use static TrueType files that permit embedding, with one entry per family
and the name declared in the files. Each face accepts a `Blob` (including
`File`), `ArrayBuffer`, or `Uint8Array`. Add `bold`, `italic`, and
`boldItalic` when available. A supplied list replaces all bundled faces,
including the code font; use `fonts: []` to embed none.

To use a font installed on the reader's machine without embedding it, set
its family name (for example, `fontFamily: "Marianne"`). For WOFF or WOFF2,
[unpack it once at build time](/docs/features/export/pdf#unpacking-webfonts)
and pass the resulting static `.ttf` file to `fonts`. For other outlines,
use the installed family or obtain a static TrueType version.

An unreadable file or unsupported format rejects the export with a
`FontEmbeddingError`. Use the family and face in the error to identify
which file to replace before retrying.
89 changes: 71 additions & 18 deletions docs/content/docs/features/export/pdf.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -150,29 +150,32 @@ diagrams as embedded vector graphics. See

### Fonts

By default, exports use a bundled font set matching the editor: Inter
(body), Geist Mono (code), New Computer Modern Math (math), and Noto Color
Emoji. To use your own fonts instead, pass the font bytes and the matching
family names to the constructor:
PDF exports bundle Inter 18pt (body), Geist Mono (code), New Computer
Modern Math, and Noto Color Emoji. Set `fonts` to replace the body, code,
and math fonts. **Noto Color Emoji remains the fallback when you supply
custom fonts.** Omit `emojiFont` to keep it. To replace it, supply your
own emoji font file and set `emojiFontFamily` to the family name in that
file. To disable the emoji fallback, set `emojiFont: []` and
`emojiFontFamily: undefined`. Set `fonts: []` to skip the bundled body,
code, and math fonts.

```typescript
const exporter = new PDFExporter(editor.schema, typstDefaultSchemaMappings, {
fontFamily: "My Font",
monoFontFamily: "My Mono Font",
// Uint8Array[], or a promise of them
fonts: [myFontBytes, myMonoFontBytes],
// An emoji-capable font; replaces the default independently of `fonts`
emojiFont: myEmojiFontBytes,
fonts: [myFontBlob, myMonoFontBlob],
});
```

Typst selects fonts by the family name embedded in the font file itself, so
the `fontFamily` options must match what your files declare. A mismatch
shows up as an `unknown font family` entry in the result's
`compileWarnings`.
Use the family names declared in the files. Typst reports unknown names
in `compileWarnings`. Pass TTF, OTF, or TTC files as `Blob` (including
`File`), `ArrayBuffer`, or `Uint8Array`. Unsupported or unreadable files
produce a `compileErrors` entry with the file's position in the body or
emoji list (starting at 1), so you can replace the offending file before
retrying. If you only have WOFF or WOFF2 files, see
[Unpacking webfonts](#unpacking-webfonts).

To extend the defaults rather than replace them (e.g. adding a CJK font as
a per-glyph fallback), spread the exported default loaders:
To add a fallback font alongside the defaults, use `loadDefaultBodyFonts()`:

```typescript
import {
Expand All @@ -188,6 +191,59 @@ const exporter = new PDFExporter(editor.schema, typstDefaultSchemaMappings, {
});
```

#### Unpacking webfonts

Use the original desktop font if available. If you only have webfonts and
know which ones you need before deployment, **decode them once at build
time**. Then pass the unpacked files to the exporters for embedding. This
saves each browser from repeating the conversion. Use static `.ttf` files
for fonts shared across PDF, DOCX, and ODT, and check that the font license
allows unpacking and embedding.

On Node.js 22.12 or later, save this script as `unpack-font.mjs` and run
`node unpack-font.mjs Font-Regular.woff2`:

```js
import { readFile, writeFile } from "node:fs/promises";
import { decodeWebFont } from "@blocknote/xl-pdf-exporter/font";

const source = process.argv[2];
const result = await decodeWebFont(await readFile(source));
if (result.error !== undefined) {
throw new Error(`Could not unpack ${source}: ${result.error}`);
}
await writeFile(
`${source.replace(/\.woff2?$/i, "")}.${result.extension}`,
result.bytes,
);
```

The script accepts either `.woff` or `.woff2`. Repeat for each face you
need. Unpacking preserves the outlines: the result is `.ttf` or `.otf`.
PDF accepts either; DOCX and ODT embedding require static TrueType.

For fonts selected at runtime, such as a user upload, the same opt-in
helper runs in the browser. It detects the format from the bytes and
accepts a `Blob` (including `File`), `ArrayBuffer`, or `Uint8Array`:

```typescript
import { decodeWebFont } from "@blocknote/xl-pdf-exporter/font";

const result = await decodeWebFont(uploadedFile);
if (result.error !== undefined) {
throw new Error(`Could not unpack the font: ${result.error}`);
}
// Pass result.bytes to the exporter's fonts option.
```

WOFF uses the runtime's `DecompressionStream` API (Chrome 80+,
Firefox 113+, Safari 16.4+, or Node.js 18+). WOFF2 loads a WebAssembly
decoder on demand and requires Node.js 22.12+ on the server. If your
browser's Content Security Policy blocks WebAssembly or runtime JavaScript
generation, WOFF2 decoding returns `unsupported-runtime`. Use the build-time
approach for known fonts in that case. The helper limits both input and
unpacked output to 30 MiB.

### Self-hosting the compiler

The Typst compiler is a ~25MB wasm file from `@blocknote/xl-typst-compiler`,
Expand Down Expand Up @@ -217,12 +273,9 @@ const defaultOptions = {
dictionary: locales.en,
// the colors used for highlighting, background colors and font colors
colors: COLORS_DEFAULT, // defaults from @blocknote/core
// font families (DEFAULT_FONT_FAMILY / DEFAULT_MONO_FONT_FAMILY) and the
// font bytes they resolve against - see "Fonts" above
// font families; omit fonts and emojiFont to use the bundled files
fontFamily: "Inter 18pt",
monoFontFamily: "Geist Mono",
fonts: loadDefaultBodyFonts(),
emojiFont: loadDefaultEmojiFont(),
// base font size in points
fontSize: 12,
// where the compiler wasm loads from - see "Self-hosting the compiler"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" office:version="1.3">
<office:font-face-decls>
<style:font-face style:name="Inter 18pt" svg:font-family="Inter 18pt" style:font-pitch="variable">
<style:font-face style:name="Inter 18pt" svg:font-family="Inter 18pt">
<svg:font-face-src>
<svg:font-face-uri xlink:href="Fonts/Inter_18pt-Regular.ttf" xlink:type="simple" loext:font-style="normal" loext:font-weight="normal">
<svg:font-face-format svg:string="truetype"></svg:font-face-format>
</svg:font-face-uri>
<svg:font-face-uri xlink:href="Fonts/Inter_18pt-Bold.ttf" xlink:type="simple" loext:font-style="normal" loext:font-weight="bold">
<svg:font-face-format svg:string="truetype"></svg:font-face-format>
</svg:font-face-uri>
<svg:font-face-uri xlink:href="Fonts/Inter_18pt-Italic.ttf" xlink:type="simple" loext:font-style="italic" loext:font-weight="normal">
<svg:font-face-format svg:string="truetype"></svg:font-face-format>
</svg:font-face-uri>
<svg:font-face-uri xlink:href="Fonts/Inter_18pt-BoldItalic.ttf" xlink:type="simple" loext:font-style="italic" loext:font-weight="bold">
<svg:font-face-format svg:string="truetype"></svg:font-face-format>
</svg:font-face-uri>
</svg:font-face-src>
</style:font-face>
<style:font-face style:name="Geist Mono" svg:font-family="Geist Mono" style:font-pitch="variable">
<style:font-face style:name="Geist Mono" svg:font-family="Geist Mono">
<svg:font-face-src>
<svg:font-face-uri xlink:href="Fonts/GeistMono-Regular.ttf" xlink:type="simple" loext:font-style="normal" loext:font-weight="normal">
<svg:font-face-format svg:string="truetype"></svg:font-face-format>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<office:document-styles xmlns:css3t="http://www.w3.org/TR/css3-text/" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" xmlns:officeooo="http://openoffice.org/2009/office" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:rpt="http://openoffice.org/2005/report" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:drawooo="http://openoffice.org/2010/draw" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" office:version="1.3">
<office:font-face-decls>
<style:font-face style:name="Geist Mono" svg:font-family="Geist Mono"/>
<style:font-face style:name="Inter 18pt" svg:font-family="Inter 18pt" style:font-pitch="variable"/>
<style:font-face style:name="Inter 18pt" svg:font-family="Inter 18pt">
<svg:font-face-src>
<svg:font-face-uri xlink:href="Fonts/Inter_18pt-Regular.ttf" xlink:type="simple" loext:font-style="normal" loext:font-weight="normal">
<svg:font-face-format svg:string="truetype"></svg:font-face-format>
</svg:font-face-uri>
<svg:font-face-uri xlink:href="Fonts/Inter_18pt-Bold.ttf" xlink:type="simple" loext:font-style="normal" loext:font-weight="bold">
<svg:font-face-format svg:string="truetype"></svg:font-face-format>
</svg:font-face-uri>
<svg:font-face-uri xlink:href="Fonts/Inter_18pt-Italic.ttf" xlink:type="simple" loext:font-style="italic" loext:font-weight="normal">
<svg:font-face-format svg:string="truetype"></svg:font-face-format>
</svg:font-face-uri>
<svg:font-face-uri xlink:href="Fonts/Inter_18pt-BoldItalic.ttf" xlink:type="simple" loext:font-style="italic" loext:font-weight="bold">
<svg:font-face-format svg:string="truetype"></svg:font-face-format>
</svg:font-face-uri>
</svg:font-face-src>
</style:font-face>
<style:font-face style:name="Geist Mono" svg:font-family="Geist Mono">
<svg:font-face-src>
<svg:font-face-uri xlink:href="Fonts/GeistMono-Regular.ttf" xlink:type="simple" loext:font-style="normal" loext:font-weight="normal">
<svg:font-face-format svg:string="truetype"></svg:font-face-format>
</svg:font-face-uri>
</svg:font-face-src>
</style:font-face>
<style:font-face style:name="Liberation Serif" svg:font-family="&apos;Liberation Serif&apos;" style:font-family-generic="roman" style:font-pitch="variable"/>
<style:font-face style:name="OpenSymbol" svg:font-family="OpenSymbol" style:font-charset="x-symbol"/>
<style:font-face style:name="PingFang SC" svg:font-family="&apos;PingFang SC&apos;" style:font-family-generic="system" style:font-pitch="variable"/>
Expand Down
2 changes: 1 addition & 1 deletion packages/xl-docx-exporter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"@blocknote/core": "workspace:^",
"@blocknote/xl-multi-column": "workspace:^",
"buffer": "^6.0.3",
"docx": "^9.9.0",
"docx": "^9.10.0",
"image-meta": "^0.2.2"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@
<w:spacing w:before="45" w:after="45" w:line="360" w:lineRule="auto"/>
</w:pPr>
<w:rPr>
<w:rFonts w:ascii="Inter" w:cs="Inter" w:eastAsia="Inter" w:hAnsi="Inter"/>
<w:rFonts w:ascii="Inter 18pt" w:cs="Inter 18pt" w:eastAsia="Inter 18pt" w:hAnsi="Inter 18pt"/>
</w:rPr>
</w:style>
<w:style w:type="paragraph" w:styleId="Heading1">
Expand Down Expand Up @@ -806,7 +806,7 @@
<w:name w:val="Verbatim Char"/>
<w:basedOn w:val="DefaultParagraphFont"/>
<w:rPr>
<w:rFonts w:ascii="GeistMono" w:cs="GeistMono" w:eastAsia="GeistMono" w:hAnsi="GeistMono"/>
<w:rFonts w:ascii="Geist Mono" w:cs="Geist Mono" w:eastAsia="Geist Mono" w:hAnsi="Geist Mono"/>
</w:rPr>
</w:style>
<w:style w:type="table" w:default="1" w:styleId="TableNormal">
Expand Down Expand Up @@ -1171,7 +1171,7 @@
<w:noProof/>
<w:wordWrap w:val="off"/>
<w:color w:val="ffffff"/>
<w:rFonts w:ascii="GeistMono" w:cs="GeistMono" w:eastAsia="GeistMono" w:hAnsi="GeistMono"/>
<w:rFonts w:ascii="Geist Mono" w:cs="Geist Mono" w:eastAsia="Geist Mono" w:hAnsi="Geist Mono"/>
</w:rPr>
</w:style>
<w:style w:type="paragraph" w:styleId="BlockQuote">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@
<w:spacing w:before="45" w:after="45" w:line="360" w:lineRule="auto"/>
</w:pPr>
<w:rPr>
<w:rFonts w:ascii="Inter" w:cs="Inter" w:eastAsia="Inter" w:hAnsi="Inter"/>
<w:rFonts w:ascii="Inter 18pt" w:cs="Inter 18pt" w:eastAsia="Inter 18pt" w:hAnsi="Inter 18pt"/>
</w:rPr>
</w:style>
<w:style w:type="paragraph" w:styleId="Heading1">
Expand Down Expand Up @@ -685,7 +685,7 @@
<w:name w:val="Verbatim Char"/>
<w:basedOn w:val="DefaultParagraphFont"/>
<w:rPr>
<w:rFonts w:ascii="GeistMono" w:cs="GeistMono" w:eastAsia="GeistMono" w:hAnsi="GeistMono"/>
<w:rFonts w:ascii="Geist Mono" w:cs="Geist Mono" w:eastAsia="Geist Mono" w:hAnsi="Geist Mono"/>
</w:rPr>
</w:style>
<w:style w:type="table" w:default="1" w:styleId="TableNormal">
Expand Down Expand Up @@ -1050,7 +1050,7 @@
<w:noProof/>
<w:wordWrap w:val="off"/>
<w:color w:val="ffffff"/>
<w:rFonts w:ascii="GeistMono" w:cs="GeistMono" w:eastAsia="GeistMono" w:hAnsi="GeistMono"/>
<w:rFonts w:ascii="Geist Mono" w:cs="Geist Mono" w:eastAsia="Geist Mono" w:hAnsi="Geist Mono"/>
</w:rPr>
</w:style>
<w:style w:type="paragraph" w:styleId="BlockQuote">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@
<w:spacing w:before="45" w:after="45" w:line="360" w:lineRule="auto"/>
</w:pPr>
<w:rPr>
<w:rFonts w:ascii="Inter" w:cs="Inter" w:eastAsia="Inter" w:hAnsi="Inter"/>
<w:rFonts w:ascii="Inter 18pt" w:cs="Inter 18pt" w:eastAsia="Inter 18pt" w:hAnsi="Inter 18pt"/>
</w:rPr>
</w:style>
<w:style w:type="paragraph" w:styleId="Heading1">
Expand Down Expand Up @@ -805,7 +805,7 @@
<w:name w:val="Verbatim Char"/>
<w:basedOn w:val="DefaultParagraphFont"/>
<w:rPr>
<w:rFonts w:ascii="GeistMono" w:cs="GeistMono" w:eastAsia="GeistMono" w:hAnsi="GeistMono"/>
<w:rFonts w:ascii="Geist Mono" w:cs="Geist Mono" w:eastAsia="Geist Mono" w:hAnsi="Geist Mono"/>
</w:rPr>
</w:style>
<w:style w:type="table" w:default="1" w:styleId="TableNormal">
Expand Down Expand Up @@ -1170,7 +1170,7 @@
<w:noProof/>
<w:wordWrap w:val="off"/>
<w:color w:val="ffffff"/>
<w:rFonts w:ascii="GeistMono" w:cs="GeistMono" w:eastAsia="GeistMono" w:hAnsi="GeistMono"/>
<w:rFonts w:ascii="Geist Mono" w:cs="Geist Mono" w:eastAsia="Geist Mono" w:hAnsi="Geist Mono"/>
</w:rPr>
</w:style>
<w:style w:type="paragraph" w:styleId="BlockQuote">
Expand Down
Loading
Loading