mirror of
https://github.com/dolanmiu/docx.git
synced 2026-11-30 16:47:55 +01:00
* feat(layout): kern text and join letters into ligatures in the width tables' fonts and fonts' files, as Word does Word kerns text that asks for it and joins letters into ligatures, and its Normal template turns both on. The width tables now have Word's kerning of every pair of the characters of Windows-1252, and the ligatures of each setting of Word's Font dialog, in Calibri, Cambria, Arial, Times New Roman and Courier New, from Word's PDF of scripts/layout-probes/word-kerning.docx, and fonts given as files join letters with their GSUB ligatures. Where Word's PDFs don't show how it kerns or joins text, the layout stops: across runs measured together, beside a soft hyphen or a character that takes no room, in a word longer than its line, and at kerning and ligatures the tables don't have. The kerning demo's 202 lines are all where Word's PDF of it puts them. * fix(layout): look for kerning and ligatures not yet followed in the parts of text between tabs, as they are measured measureTextWidthAsDrawn measures the parts of text either side of a tab typed in it apart, but unknownShaping left the tabs out and looked at the text as one, so it checked pairs and ligatures across a tab the measurement never has, and could miss a pair it does. It now checks each part, as the font-file measurer does. Raised by CodeRabbit's review of #3661.
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 💖
