DOCX
@office-open/docx
Generate .docx documents with a declarative TypeScript API
Installation
pnpm add @office-open/docx
Quick Start
import { Document, Packer, Paragraph, TextRun } from "@office-open/docx";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [new TextRun("Hello World")],
}),
],
},
],
});
const buffer = await Packer.toBuffer(doc);
Main Components
| Component | Description |
|---|---|
Document | Root document container with sections and properties |
Paragraph | Text paragraph with formatting, alignment, and spacing |
TextRun | Inline text with font, size, color, and style |
Table, TableRow, TableCell | Structured tables with merged cells and borders |
ImageRun | Embedded images with transformation and positioning |
ChartRun | Bar, line, pie, area, and scatter charts |
SmartArtRun | Predefined SmartArt diagrams |
Header, Footer | Section headers and footers |
FootnoteReferenceRun | Footnotes and endnotes |
TableOfContents | Auto-generated table of contents |
Bookmark | Internal bookmarks and cross-references |
Math, MathRun | Mathematical equations |
Styles | Declarative style definitions |
Export Formats
| Method | Returns | Use Case |
|---|---|---|
Packer.toBuffer(doc) | Uint8Array | Node.js file I/O |
Packer.toBlob(doc) | Blob | Browser downloads |
Packer.toBase64String(doc) | string | Data URLs, API payloads |
Packer.toString(doc) | string | Debugging, inspection |
Packer.toStream(doc) | ReadableStream | Streaming large files |