Files
docx/docs/usage/fonts.md
T
DolanandClaude Opus 5.5 c169dd1a83 feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded (#3556)
* feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded

The docs' code examples were plain snippets, often fragments that couldn't run. A code block marked ```ts live is now
an example readers can see and change: the page runs it with docx, draws the document it makes next to the code with
docx-preview, and offers the .docx to download. Clicking the code opens an editor (Monaco) that knows docx's types,
the pages are drawn again as the code changes, and View closes the editor. Split, Code and Preview show the code and
the pages side by side, or one of them. The demos a page includes are live too.

Every example in the docs that builds or patches a document is now live: 443 of them, each a whole file with its
imports, so it runs as written. Code about other things, such as installing, bundlers or servers, stays plain.

Running them showed docs that were wrong, fixed here:
- Landscape: docx swaps a landscape page's width and height itself, so the docs' swapped sizes made a tall page
  marked as landscape
- Comments: `comments.children` takes plain objects, not `new Comment(...)`
- Custom properties are text only
- APIs that no longer exist: MathText, Row, `.then` on Packer.toStream, patchDocument(buffer, ...),
  createParagraph().heading1(), a border's `value`, TextRun's `.break()`
- The templates and patcher examples read files that aren't in the repository. They now patch the templates in
  demo/assets, or make their own

`npm run build.docs` copies docx's browser builds into docs/live-examples/lib, so the docs use the docx they
describe, and the Pages workflow runs it. The folder is gitignored, and cspell and ESLint skip it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* docs: the bullet examples draw a bullet, and the patcher's usage writes the patched document

"ὠ" is U+1F60, a Greek omega, since \u takes four hex digits, so the two live bullet examples on
the numbering page drew it as each item's bullet. They use "•" now. The patcher's usage example
now awaits patchDocument and writes the document it makes, as the example after it does.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-28 13:00:29 +01:00

9.6 KiB

Custom Fonts

Custom fonts allow you to embed fonts directly into your document, ensuring consistent appearance across different systems.

Embedding Fonts

Embed a font by providing the font data in the Document constructor:

import * as fs from "fs";
import { CharacterSet, Document, Paragraph, TextRun } from "docx";

const fontData = fs.readFileSync("./demo/assets/Pacifico.ttf");

const doc = new Document({
    fonts: [
        {
            name: "Pacifico",
            data: fontData,
            characterSet: CharacterSet.ANSI,
        },
    ],
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new TextRun({
                            text: "This text uses a custom font",
                            font: "Pacifico",
                        }),
                    ],
                }),
            ],
        },
    ],
});

Font Names

The name property can contain spaces and non-ASCII characters (e.g. "EB Garamond", "Noto Sans JP"). Internally, docx uses sequential filenames (font1.odttf, font2.odttf, …) for the embedded font data in the zip package, so the font name you choose has no effect on file compatibility.

Font Options

Property Type Notes Description
name string Required Font name to reference
data Buffer Required Font file data
characterSet CharacterSet Optional Character set for the font

Character Sets

The characterSet property controls the w:charset element in fontTable.xml, which tells Word how to interpret the font's character encoding. While optional, specifying the correct character set is important for non-Latin scripts — without it, Word may fall back to a default encoding and render characters incorrectly.

Available character sets:

Value Description
CharacterSet.ANSI Standard ANSI characters
CharacterSet.DEFAULT Default character set
CharacterSet.SYMBOL Symbol characters
CharacterSet.MAC Macintosh characters
CharacterSet.SHIFTJIS Japanese Shift-JIS
CharacterSet.HANGUL Korean Hangul
CharacterSet.JOHAB Korean Johab
CharacterSet.GB2312 Simplified Chinese
CharacterSet.CHINESEBIG5 Traditional Chinese
CharacterSet.GREEK Greek characters
CharacterSet.TURKISH Turkish characters
CharacterSet.VIETNAMESE Vietnamese characters
CharacterSet.HEBREW Hebrew characters
CharacterSet.ARABIC Arabic characters
CharacterSet.BALTIC Baltic characters
CharacterSet.RUSSIAN Cyrillic characters
CharacterSet.THAI Thai characters
CharacterSet.EASTEUROPE Eastern European

Using Custom Fonts

In TextRun

import * as fs from "fs";
import { CharacterSet, Document, Paragraph, TextRun } from "docx";

const fontData = fs.readFileSync("./demo/assets/Pacifico.ttf");

const doc = new Document({
    fonts: [{ name: "Pacifico", data: fontData, characterSet: CharacterSet.ANSI }],
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new TextRun({
                            text: "Custom font text",
                            font: "Pacifico",
                        }),
                    ],
                }),
            ],
        },
    ],
});

In Paragraph Style

import * as fs from "fs";
import { CharacterSet, Document, Paragraph, TextRun } from "docx";

const fontData = fs.readFileSync("./demo/assets/Pacifico.ttf");

const doc = new Document({
    fonts: [{ name: "Pacifico", data: fontData, characterSet: CharacterSet.ANSI }],
    sections: [
        {
            children: [
                new Paragraph({
                    run: {
                        font: "Pacifico",
                    },
                    children: [new TextRun("All text in this paragraph uses the custom font")],
                }),
            ],
        },
    ],
});

In Document Styles

import * as fs from "fs";
import { CharacterSet, Document, Paragraph } from "docx";

const fontData = fs.readFileSync("./demo/assets/Pacifico.ttf");

const doc = new Document({
    fonts: [{ name: "Pacifico", data: fontData, characterSet: CharacterSet.ANSI }],
    styles: {
        default: {
            document: {
                run: {
                    font: "Pacifico",
                },
            },
        },
    },
    sections: [
        {
            children: [new Paragraph("All text in the document uses the custom font")],
        },
    ],
});

As the Theme's Font

A font can also be one of the fonts of the document's theme, so text in it changes font when the theme's fonts change:

import * as fs from "fs";
import { CharacterSet, Document, Paragraph } from "docx";

const fontData = fs.readFileSync("./demo/assets/Pacifico.ttf");

const doc = new Document({
    fonts: [{ name: "Pacifico", data: fontData, characterSet: CharacterSet.ANSI }],
    theme: { fonts: { body: "Pacifico" } },
    styles: {
        default: {
            document: {
                run: {
                    font: { theme: "body" },
                },
            },
        },
    },
    sections: [
        {
            children: [new Paragraph("All text in the document uses the theme's font for body text")],
        },
    ],
});

Multiple Fonts

Embed multiple fonts:

import * as fs from "fs";
import { CharacterSet, Document, Paragraph, TextRun } from "docx";

const doc = new Document({
    fonts: [
        {
            name: "HeadingFont",
            // The demo folder has one font, so both are Pacifico here: give each its own font file
            data: fs.readFileSync("./demo/assets/Pacifico.ttf"),
            characterSet: CharacterSet.ANSI,
        },
        {
            name: "BodyFont",
            data: fs.readFileSync("./demo/assets/Pacifico.ttf"),
            characterSet: CharacterSet.ANSI,
        },
    ],
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new TextRun({
                            text: "Heading",
                            font: "HeadingFont",
                            size: 48,
                        }),
                    ],
                }),
                new Paragraph({
                    children: [
                        new TextRun({
                            text: "Body text with a different font",
                            font: "BodyFont",
                        }),
                    ],
                }),
            ],
        },
    ],
});

Mixing System and Custom Fonts

You can mix embedded fonts with system fonts:

import * as fs from "fs";
import { CharacterSet, Document, Paragraph, TextRun } from "docx";

const fontData = fs.readFileSync("./demo/assets/Pacifico.ttf");

const doc = new Document({
    fonts: [{ name: "Pacifico", data: fontData, characterSet: CharacterSet.ANSI }],
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new TextRun({
                            text: "System font (Arial), ",
                            font: "Arial",
                        }),
                        new TextRun({
                            text: "Custom embedded font",
                            font: "Pacifico",
                        }),
                    ],
                }),
            ],
        },
    ],
});

Browser Usage

In browsers, convert your font file to a base64 string or ArrayBuffer:

import { CharacterSet, Document, Packer, Paragraph, TextRun } from "docx";
import { saveAs } from "file-saver";

// Fetch the font file and convert to ArrayBuffer
const response = await fetch("./fonts/MyFont.ttf");
const fontData = await response.arrayBuffer();

const doc = new Document({
    fonts: [
        {
            name: "MyFont",
            data: Buffer.from(fontData),
            characterSet: CharacterSet.ANSI,
        },
    ],
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new TextRun({
                            text: "Text with embedded font",
                            font: "MyFont",
                        }),
                    ],
                }),
            ],
        },
    ],
});

// Export the document
Packer.toBlob(doc).then((blob) => {
    saveAs(blob, "document.docx");
});

Supported Font Formats

  • TTF (TrueType Font) - Most common, widely supported
  • OTF (OpenType Font) - Also supported

?> For best compatibility, use TTF fonts when possible.

Demo

Embedding Custom Fonts

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/text/custom-fonts.ts

Declarative Custom Fonts

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/text/custom-fonts-in-styles.ts