DOCX

Quick Start

Create your first .docx document in 5 minutes with @office-open/docx

Create a Word document from scratch in three steps: define content, build a document, and export.

Step 1 — Install

pnpm add @office-open/docx

Step 2 — Create a Document

import { Document, Packer, Paragraph, TextRun } from "@office-open/docx";

const doc = new Document({
    sections: [
        {
            children: [
                // A title paragraph
                new Paragraph({
                    heading: "Heading1",
                    children: [new TextRun({ text: "My First Document", bold: true, size: 32 })],
                }),

                // A body paragraph
                new Paragraph({
                    children: [
                        new TextRun("This is a paragraph with "),
                        new TextRun({ text: "bold", bold: true }),
                        new TextRun(" and "),
                        new TextRun({ text: "italic", italics: true }),
                        new TextRun(" text."),
                    ],
                }),
            ],
        },
    ],
});

Step 3 — Export

// Node.js — write to file
import { writeFileSync } from "node:fs";

const buffer = await Packer.toBuffer(doc);
writeFileSync("output.docx", buffer);
// Browser — download as file
const blob = await Packer.toBlob(doc);
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "output.docx";
a.click();

Output Formats

The Packer class supports multiple output formats:

MethodReturnsUse Case
Packer.toBuffer(doc)Uint8ArrayNode.js file I/O
Packer.toBlob(doc)BlobBrowser downloads
Packer.toBase64String(doc)stringData URLs, API payloads
Packer.toString(doc)stringDebugging, inspection
Packer.toStream(doc)ReadableStreamStreaming large files
Copyright © 2026