DOCX
Export
Export documents to buffer, blob, base64, string, or stream
The Packer class converts a Document into various output formats.
toBuffer
Returns a Uint8Array. Best for Node.js file I/O.
import { Packer } from "@office-open/docx";
import { writeFileSync } from "node:fs";
const buffer = await Packer.toBuffer(doc);
writeFileSync("output.docx", buffer);
toBlob
Returns a Blob. Best for browser environments.
const blob = await Packer.toBlob(doc);
const url = URL.createObjectURL(blob);
// Trigger download
const a = document.createElement("a");
a.href = url;
a.download = "output.docx";
a.click();
URL.revokeObjectURL(url);
toBase64String
Returns a Base64-encoded string. Useful for data URLs or API payloads.
const base64 = await Packer.toBase64String(doc);
const dataUrl = `data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,${base64}`;
toString
Returns a string representation. Useful for debugging.
const str = await Packer.toString(doc);
console.log(str);
toStream
Returns a ReadableStream. Useful for streaming large documents.
import { createWriteStream } from "node:fs";
const stream = await Packer.toStream(doc);
const writeStream = createWriteStream("output.docx");
stream.pipeTo(writeStream);
Summary
| Method | Return Type | Environment | Use Case |
|---|---|---|---|
toBuffer | Uint8Array | Node.js | File I/O |
toBlob | Blob | Browser | Downloads |
toBase64String | string | Any | Data URLs, APIs |
toString | string | Any | Debugging |
toStream | ReadableStream | Node.js | Large files |