DOM-docx – Convert semantic HTML to native, editable Word documents
Show HN: DOM-docx – HTML to native, editable Word docs (MIT)
DOM-docx is a powerful open-source library that transforms semantic HTML fragments into fully native, editable Microsoft Word documents (OOXML). Unlike tools that generate static screenshots or rely on layout hacks, this library preserves paragraphs, lists, tables, and images with high fidelity. It supports both Node.js and browser environments, offering a lightweight pure JavaScript path for inline styles or an optional Playwright integration for complex computed styles. Developers can easily integrate it via npm or CLI to generate professional reports, invoices, and documents directly from web content.
Convert semantic HTML fragments to native, editable Word documents (OOXML): paragraphs, runs, lists, tables, images. Not screenshots or layout hacks.
- fishbone
Hey HN, author here.
I do a lot of backend document (docx file) generation work, and updating our templates and backend code is one of my least favorite development tasks. Cryptic errors, minute-plus rebuild loops. I’d much prefer building these reports in JS rendered HTML (e.g., Vue or React), but existing HTML-to-docx libraries in the OSS ecosystem don't produce output that's actually valid, editable Word structure.
I'd had good luck applying Karpathy's Autoresearch pattern (agent runs iterations against an objective score, keeps what improves, discards what doesn't) to a couple of other problems, and figured OOXML fidelity was a good fit.
The Autoresearch process goes like this: render HTML and take a screenshot, use dom-docx to convert to docx, rasterize the docx file with LibreOffice and take another screenshot, score the browser HTML screenshot vs LibreOffice screenshot and measure layout fidelity + editability + speed as a quality metric, feed score back in, and repeat to drive higher fidelity within the constraints of editability and performance. It’s a beautiful thing to watch.
Burned some tokens and ran that loop against 37 real-world HTML patterns such as nested lists, tables, flex layouts and blockquotes (stuff that often breaks converters) and "brute forced" my way to what I hope is a high-fidelity HTML to docx converter.
A few things about where it landed:
- Native OOXML output, real Word structure, not a screenshot or a 1x1 table pretending to be a document
- W […]
- ape4
Just adding, since I don't see it mentioned in the readme, that its written in Typescript. That's what makes this interesting. I imagine Pandoc can do this but its not Typescript (its Haskell).
- jkwang
The screenshot-to-docx scoring loop is a clever way to verify layout fidelity. Very useful for anyone generating reports from HTML.
- conwy
Awesome, thanks so much for open-sourcing!
Will use it to generate my C.V. in Word format.
- virajk_31
I have done something similar for PPTX, keeping the fidility intact was really challenging with computed values & OOXML counterparts and again challanges with different XML implementations like that of MS & Libre..