// HTML as a document's content (issue #2632): a rich-text editor's HTML, with its headings, formatting, lists, a // table, an image, links, math and a page break, in a document's body, and some inline HTML in its header and a // paragraph of its own. See docs/usage/html.md. // cspell:ignore EMEA import * as fs from "fs"; import { Document, Header, Packer, Paragraph, TextRun } from "docx"; import { htmlToContent, htmlToRuns } from "docx/html"; const html = `

Quarterly report

Sales were up 12%, led by EMEA. Returns were flat, and costs rose fell.

Highlights

  1. New customers: 48
  2. Renewals: 97%
RegionQ2Q3
EMEA1,0501,200
APAC880900
Total2,100

The company's logo

The best quarter since the company started.

Growth, compounded: .

Appendix

total = sum(sales)

Back to the appendix.

`; const doc = new Document({ sections: [ { headers: { default: new Header({ children: [new Paragraph({ children: [...htmlToRuns("Quarterly report · Draft")] })], }), }, children: [ ...htmlToContent(html, { images: { "logo.png": fs.readFileSync("./demo/assets/html/logo.png") } }), new Paragraph({ children: [new TextRun("Written by docx: "), ...htmlToRuns("bold and italic in a paragraph of its own.")], }), ], }, ], }); Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); });