Files
docx/docs/usage/packers.md
T
DolanandClaude Opus 5.5 e0ff13ee3d chore: organise the demos into a folder for each topic (#3552)
* chore: organise the demos into a folder for each topic

The demos were one flat list of 124 numbered files, which made it hard to
find the one you wanted. Each is now in a folder for its topic, such as
demo/tables/basic-table.ts, with a name that says what it shows instead of
a number.

npm run demo lists every demo and narrows the list as you type. It also
takes a demo's path or name, a topic, or search words.

The demos' images moved to demo/assets/images, as images is now a topic.
The docs' links, the Demos workflow, the shape render script and its
reference images use the new paths and names.

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

* fix: list only a topic's own demos, and drop stale output file names

npm run demo text also offered the text-boxes demos and others with
"text" in their name, so a topic now searches for "text/". Three demos
logged their old numbered file name after writing My Document.docx.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

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

2.7 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.

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

Export as string

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

Export as a base64 string

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

Packer.toStream(doc).then((stream) => {
    // 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.

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.

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