mirror of
https://github.com/dolanmiu/docx.git
synced 2026-11-30 16:47:55 +01:00
* feat(layout): lay out equations Word builds up, as Word does Lay out the equations Word builds up (fractions, scripts, roots, sums and integrals with their limits, brackets that grow, matrices and cases, accents, bars, functions and limits, and braces) by the rules of Cambria Math's OpenType MATH table, with the font's own widths, ink and constants, as Word's PDFs of word-stops-equations, -equation-settings and -equation-font show. Each probe's width in its line of text is Word's to within 0.03 points and its line's height within Word's own drawing grid, but two roots, which are within 0.1 points. What of Cambria Math the layout needs is generated from Word's own copy of the font by scripts/generate-cambria-math.ts into src/layout/cambria-math.ts, which replaces the widths read from Word's PDFs (equation-widths.ts) and the table of Cambria Math's own widths within 1.5 thousandths of an em, the integrals with their italic correction, and ◇, which Cambria Math hasn't, stops. The layout keeps stopping where Word's PDFs leave the result unsettled: scripts in sizes other than 11 points, superscripts in cramped parts, brackets between two sizes, matrices whose rows are too tall to be single spaced, thin spaces beside fractions, brackets, sums and functions, boxes, normal text, equations too long for their line, maths fonts other than Cambria Math, margins of displayed equations and space between equations side by side in one m:oMathPara. Guessing past a stop, an equation alone in its paragraph is laid out displayed first. word-stops-equations2.ts writes the probes that settle them, each validated against ooxml-schemas/. * fix(layout): stop at a digit in a bold run or another alphabet, which Word hasn't been seen drawing A digit in a bold or bold italic run was laid out as the plain digit, though Word draws a bold run's letters in Unicode's bold alphabet (word-stops-equations.docx EQ19), which has bold digits too, wider than the plain ones. No probe has shown which Word draws, so the layout stops there, as it does for a digit in another alphabet, and word-stops-equations2.ts's EQ45 asks Word. The alphabets' unused first digits are gone. * fix(layout): lay out an equation in a line of text in a numbered paragraph, stopping only at one alone after its list's number * test(layout): probe an equation displayed (m:oMathPara) alone after its list's number too, as EQ38d
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 💖
