Files
docx/docs/usage/packers.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

3.8 KiB

Packers

Packers are the way in which docx turns your code into .docx format. It is completely decoupled from the docx.Document.

Packers works in both a node and browser environment (Angular etc). Now, the packer returns a Buffer, Blob, string, base64 string, ArrayBuffer, or Stream. It is up to you to take that and persist it with node's fs, send it down as a downloadable file, or anything else you wish. As of version 4+, this library will not have options to export to PDF.

Export as Buffer

This will return a NodeJS Buffer. If this is used in the browser, it will return a UInt8Array instead.

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

const doc = new Document({
    sections: [
        {
            children: [new Paragraph("Hello World")],
        },
    ],
});

Packer.toBuffer(doc).then((buffer) => {
    fs.writeFileSync("My Document.docx", buffer);
});

Export as string

import { Document, Packer, Paragraph } from "docx";

const doc = new Document({
    sections: [
        {
            children: [new Paragraph("Hello World")],
        },
    ],
});

Packer.toString(doc).then((string) => {
    console.log(string);
});

Export as a base64 string

import { Document, Packer, Paragraph } from "docx";

const doc = new Document({
    sections: [
        {
            children: [new Paragraph("Hello World")],
        },
    ],
});

Packer.toBase64String(doc).then((string) => {
    console.log(string);
});

Export as Blob

This is useful if you want to send it as an downloadable in a browser environment.

Packer.toBlob(doc).then((blob) => {
    // saveAs from FileSaver will download the file
    saveAs(blob, "example.docx");
});

Export as ArrayBuffer

This may be useful when working in a Node.js worker.

Packer.toArrayBuffer(doc).then((arrayBuffer) => {
    port.postMessage(arrayBuffer, [arrayBuffer]);
});

Export as a Stream

import { Document, Packer, Paragraph } from "docx";

const doc = new Document({
    sections: [
        {
            children: [new Paragraph("Hello World")],
        },
    ],
});

const stream = Packer.toStream(doc);
// read from stream

Export using optional arguments

The Packer methods support 2 optional arguments.

The first is for controlling the indentation of the xml and should be a boolean or keyof typeof PrettifyType.

The second is an array of subfile overrides ({path: string, data: string}[]). These overrides can be used to write additional subfiles to the result or even override default subfiles in the case that the default handling of these subfiles does not meet your needs.

import { Document, Packer, Paragraph } from "docx";

const doc = new Document({
    sections: [
        {
            children: [new Paragraph("Hello World")],
        },
    ],
});

const overrides = [{ path: "word/commentsExtended.xml", data: "string_data" }];
Packer.toString(doc, true, overrides).then((string) => {
    console.log(string);
});

Export to arbitrary formats

You can also use the lower-level Packer.pack method to export to any specified type.

import { Document, Packer, Paragraph } from "docx";

const doc = new Document({
    sections: [
        {
            children: [new Paragraph("Hello World")],
        },
    ],
});

Packer.pack(doc, "string").then((string) => {
    console.log(string);
});

Demos

Export to Base64 Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/exporting/base64-string.ts

Node.js Stream Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/exporting/node-stream.ts