Mammoth docx to HTML alignment not preserved

1 week ago 25
ARTICLE AD BOX

I am converting a DOCX file into HTML using Mammoth.js, and most formatting is preserved correctly (bold, italic, underline, tables, images, etc.).

But paragraph alignment and list item alignment (center, right, justify) are not consistently preserved. Sometimes Mammoth keeps the alignment, sometimes it loses it.

I already know that Mammoth does not automatically preserve alignment unless explicitly mapped. I also tried extracting alignment manually from word/document.xml using JSZip + xml2js, but the number of HTML <p> tags does not always match the number of DOCX <w:p> nodes, which breaks index-based mapping.

I need a working code snippet that:

Reads DOCX paragraph alignment (including style inheritance) Converts DOCX → HTML using Mammoth Re-applies correct alignment to every HTML paragraph and list item Works even when DOM count != DOCX <w:p> count
Read Entire Article