Skip to content
lit-docx
Developer docs
Preview · unreleased

Generating DOCX

Build LitAST with hand-written nodes or the docx authoring helpers, then produce complete DOCX bytes with generateDocx; includes unit conventions.

ts

generateDocx takes no other arguments. It builds a whole DOCX package from the LitAST and returns the bytes of a complete file; it never patches an existing file in place.

Writing the bytes

ts

Hand-written nodes

LitAST nodes are plain objects, and the root is a document:

ts

Generating this document and reading it back with parseDocx yields exactly the same children.

Using the docx helpers

docx is a grouped helper object. It returns nodes like the ones above, only shorter to write and type-checked:

GroupContents
docx.blocksparagraph, heading, blockquote, codeBlock, pageBreak, callout
docx.inlinetext, link, ruby
docx.marksbold, highlight, link, textStyle
docx.tablestable, row, cell, shadedCell
docx.stylesparagraphProperties, spacing, indentation, shading, border, runProperties

The helpers do not cover every node. The document root, image, section and list are written by hand, and the two styles mix freely. Marks without a helper, such as italic, are written as { type: "italic" }.

ts

Phonetic guides

docx.inline.ruby produces native w:ruby. base is the body text and annotation is the phonetic text; each layer can carry its own formatting. The caller supplies the pronunciation; lit-docx does not include an automatic pinyin dictionary.

ts

All five properties are required when authoring. Numeric hps, hpsRaise and hpsBaseText values are half-points. hpsRaise is the stored raise, not the distance shown in Word's Phonetic Guide dialog. This example follows a Word 16.109.2 sample with 12pt base text and 6pt annotation; it is not an automatic spacing algorithm for arbitrary fonts. Keep the outer marks size, base-text size and hpsBaseText consistent; the reference size does not replace run formatting. Alignment values are center, left, right, distributeLetter, distributeSpace and rightVertical.

Parsed nodes store base text in children and annotation in attrs.rubyText. Edit those nodes or properties before generating again. Unchanged or unmodeled source properties follow the preservation rules. Place a hyperlink in the complete ruby node's marks; do not put docx.inline.link directly in either text layer.

Tables

Tables nest as table > tableRow > tableCell > paragraph. A cell needs at least one block; cells created with the helper get an empty paragraph automatically.

ts

Images, sections, headers and footers

  • Images: { type: "image", attrs: { src, width, height } }. src is the image data URL; width and height are in pixels at 96 DPI.
  • Sections: a section is a sibling inside document.children. It ends the section formed by the content before it and carries that section's page setup.
  • Headers and footers: children of a section. A header uses attrs.headerType and a footer uses attrs.footerType, each "default", "first" or "even".

The report in Quick start uses all three.

Units

ValueUnit
Page size, margins, paragraph indents, spacing before and after, table column widthstwips, 1/20 of a point; 1 inch = 1440, 1 cm ≈ 567
Image width and heightpixels at 96 DPI
fontSize in textStylea string such as "12pt", or a number of points
Ruby hps, hpsRaise, hpsBaseTextNumeric half-points, or preserved explicit OOXML measurement strings
Colors6-digit hex RGB; "C00000" and "#C00000" both work; parsed results include #

A4 is pageWidth: 11906, pageHeight: 16838; US Letter is 12240 × 15840.

Parts added for new documents

A document built from scratch has no original styles.xml, numbering.xml and similar parts, so the generator adds the numbering, comment and note parts the content actually uses. LitAST read from an existing file writes back the original parts kept in ast.meta.rawXmlFiles; see Round trips and preservation.

lit-docx · QuireKitA DOCX parser and generator, licensed under AGPL-3.0 with a commercial option.