mirror of
https://github.com/dolanmiu/docx.git
synced 2026-12-01 16:55:59 +01:00
* feat(layout): widen a column for a word longer than its cells give it, as Word does In a table whose cells all have widths, Word makes a column as wide as its longest word, grows a table with no width of its own up to the room, keeps the width of one with its own, and narrows the other columns toward their longest words, each by its share of the width they can give up. Probed in scripts/layout-probes/word-long-words.ts. A word longer than the table can make room for, a long word in a table wider than its cells, and one in a table with cells merged across columns still stop the layout. * chore(layout): keep Word's PDF of word-long-words beside it, read by word-autofit.py * docs(layout): list word-long-words among the layout probes * fix(layout): lay out a paragraph kept with a table whose columns can't be widened before stopping there The lookahead for a paragraph kept with the next sized the table, which threw when Word's widening of it isn't known, so the layout stopped before placing the paragraph and its bookmark went without a page number. It now stops only where the table is placed, as it does for a table it can't read.
Welcome to docx
What is docx?
docx is a TypeScript/JavaScript library for generating Word documents (.docx files) programmatically. It provides a declarative API that works seamlessly in both Node.js and browser environments.
Why docx?
- Declarative API - Define what you want (objects/config), not how to build it step-by-step
- TypeScript First - Full type definitions with IntelliSense support
- Universal - Works in Node.js, browsers, and serverless environments
- No Dependencies - Zero external runtime dependencies
- Full Featured - Tables, images, shapes, charts, headers, footers, styles, and more
Quick Navigation
| I want to... | Go to... |
|---|---|
| Get started quickly | Quickstart Guide |
| Learn a specific feature | Usage Guides |
| See working examples | Demo Files |
| Browse the API | API Documentation |
| Modify existing documents | Patcher |
Installation
npm install --save docx
Then you can require or import as usual:
const docx = require("docx");
import * as docx from "docx";
// or
import { Document, Packer, Paragraph, TextRun } from "docx";
Shapes, watermarks and charts come with the package too, from docx/shapes, docx/watermarks and docx/charts:
import { ShapeRun } from "docx/shapes";
import { TextWatermark } from "docx/watermarks";
import { ChartRun } from "docx/charts";
Basic Usage
import * as fs from "fs";
import { Document, Packer, Paragraph, TextRun } from "docx";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new TextRun("Hello World"),
new TextRun({
text: " - Bold text",
bold: true,
}),
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});
Made with 💖
