Files
docx/docs
Dolan 64e84bf387 feat(layout): lay out words longer than their table can make room for, and long words beside space between cells, as Word does (#3681)
* feat(layout): lay out words longer than their table can make room for, and long words beside space between cells, as Word does

Word's PDF of word-stops-long-words.docx (lay-stops2's batch) settles most of
the work's line 9:

- Where a table's columns' widest words don't fit in the room, and it has no
  width of its own in twips, Word gives each column's text a share of the
  room in proportion to its widest word, whatever width its cells give it,
  and breaks the words (LW1a to LW1l, LW4d; also L7, TW12 and TW13, which
  had been read as fitting no rule).
- A long word with more text beside it in a cell merged across columns
  widens them as its line does (LW2a, LW2b).
- With space between cells, a column with a long word is widened for it, and
  a table wider than its columns has them widened in proportion to fill it,
  the space aside (LW3a to LW3f, LW4a to LW4c); rows that give a column
  different widths are evened out as without the space (LW5c).

A table whose width is a share of more than the width it is in now stops:
the batch's probes of shares gave 50 times what they meant, and Word laid
those out three ways. word-stops-long-words2.ts asks again for the next
batch, with the shares meant.

* fix(layout): stop at a table whose cells' margins are wider than the room for it

Sharing the room among the columns in proportion to their widest words
gave widths that weren't numbers for empty cells, and below nothing for
others, where the cells' margins alone are wider than the room, such as
a table in a narrow cell. master stopped there, and what Word does
hasn't been seen, so the layout stops again, sizing each column as wide
as its widest word for a layout that guesses past it.
2026-10-05 14:33:25 +01:00
..
2020-09-05 18:40:40 +01:00
2020-09-05 18:51:50 +01:00

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);
});

clippy the assistant


Made with 💖