docx
    Preparing search index...

    Function htmlToContent

    • Turns HTML into paragraphs and tables, to go in a document's sections, headers, footers, table cells and anywhere else they go, among others, or to patch a template with.

      It reads what rich-text editors write, such as TinyMCE, CKEditor, Quill, TipTap and Lexical, and what Word and Google Docs put on the clipboard, as a browser reads it: its <style> elements and style attributes, the styles of the classes those editors write, and HTML's own meanings, such as <b>'s bold. Paragraphs, headings, lists and links are in the Word document's own styles, such as Heading 1 and Hyperlink, with only what the HTML says, such as a colour or a font size, written as direct formatting.

      It has headings, paragraphs and line breaks; bold, italic, underlined, struck through, superscript and subscript text, its colours, backgrounds, fonts, sizes and spacing; alignment, indents, line spacing, borders and shading of paragraphs; lists, numbered or not, in each other, with their numbers' styles and starts; tables, with merged cells, header rows, widths, borders, backgrounds and alignment; images, with their sizes and alt text, floating or not; links, within the document too; checkboxes; math, as Word's equations; page breaks; and tracked changes.

      Nothing is downloaded: images that aren't data URLs are given with the images option. HTML that isn't valid is read as a browser reads it, and what the document can't have, such as a video, is left out, with a warning to onWarning. It never throws for the HTML it is given.

      Parameters

      • html: string

        The HTML: some of a document's body, such as an editor's content, or a whole document

      • options: HtmlOptions = {}

        Options, such as the images and CSS of the HTML

      Returns readonly (Paragraph | Table)[]

      The paragraphs and tables

      import { Document, Packer } from "docx";
      import { htmlToContent } from "docx/html";

      const doc = new Document({
      sections: [{ children: htmlToContent("<h1>Report</h1><p>The <b>results</b> are in.</p>") }],
      });
      const buffer = await Packer.toBuffer(doc);