<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Blair Googer</title>
    <description>The latest articles on DEV Community by Blair Googer (@blair_googer_8e41a7d338d2).</description>
    <link>https://dev.to/blair_googer_8e41a7d338d2</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4026514%2F4f75a0ae-6e8a-4d48-9578-e2e6b51edc9b.png</url>
      <title>DEV Community: Blair Googer</title>
      <link>https://dev.to/blair_googer_8e41a7d338d2</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/blair_googer_8e41a7d338d2"/>
    <language>en</language>
    <item>
      <title>Brute forcing my way to better HTML &gt; DOCX conversion</title>
      <dc:creator>Blair Googer</dc:creator>
      <pubDate>Mon, 13 Jul 2026 01:31:10 +0000</pubDate>
      <link>https://dev.to/blair_googer_8e41a7d338d2/brute-forcing-my-way-to-better-html-docx-conversion-4ffj</link>
      <guid>https://dev.to/blair_googer_8e41a7d338d2/brute-forcing-my-way-to-better-html-docx-conversion-4ffj</guid>
      <description>&lt;p&gt;About 10 years ago I fell in love with Vue.js. The easy-to-learn syntax and reactivity were a joy to work with, and HMR (hot module replacement) added up to an incredible developer experience (DX).&lt;/p&gt;

&lt;p&gt;At this point I've spent thousands of hours working with front-end JavaScript, and I'm still in love with it, but I also have to do a lot of backend work.&lt;/p&gt;

&lt;p&gt;As joyful as the modern frontend DX is (shoutout Vite), working on the backend feels cumbersome by comparison. One of the tasks I like least is backend generation of Word documents using templates and server-side libraries. The iteration loop is slow, the errors are cryptic and debugging is painful.&lt;/p&gt;

&lt;p&gt;I'd much rather build documents in HTML. But the existing HTML-to-docx libraries leave a lot to be desired and I never had the motivation to build something better, until now. Enter &lt;a href="https://github.com/karpathy/autoresearch" rel="noopener noreferrer"&gt;Karpathy Autoresearch&lt;/a&gt; loops.&lt;/p&gt;

&lt;p&gt;Autoresearch loops are a general concept: use an agent to kick off a recursive self-improvement loop against something that can be objectively scored. For example, say you have a slow SQL query (speed being the metric). You could kick off a loop with a prompt like:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You are a SQL performance researcher. Run the following SQL query to establish a baseline, then come up with hypotheses to improve performance. Score each result and run 5 iterations. Avoid any regressions, each result must contain the exact same rows and columns.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This isn't exactly brute forcing, but it's a similar concept of autonomously run iterations trying to improve within a constrained task.&lt;/p&gt;

&lt;p&gt;After having success with Autoresearch loops on a couple of other things, the idea hit me. &lt;strong&gt;Can I brute-force my way to a higher-fidelity HTML-to-DOCX converter?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I sat on the idea for a few days, then just decided to try it. Enter my first open source package, &lt;strong&gt;dom-docx&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How the loop actually worked
&lt;/h2&gt;

&lt;p&gt;The scoring loop needed a way to judge "is this a good Word document" that a machine could evaluate without a human eyeballing every output. The approach:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Render a batch of HTML test cases (headings, lists, tables, flex rows, images, inline formatting, real patterns, not toy examples)&lt;/li&gt;
&lt;li&gt;Convert each to &lt;code&gt;.docx&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Rasterize the result via LibreOffice and compare layout structure against a fidelity metric comparing the rendered HTML vs the converted docx file&lt;/li&gt;
&lt;li&gt;Score each case: 50% visual/layout fidelity, 35% editability (real Word structure, not a 1×1 layout table pretending to be a document), 15% compile speed&lt;/li&gt;
&lt;li&gt;Feed regressions back in, adjust the conversion logic, repeat&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;No single clever trick made this work. It was mostly refusing to accept "good enough" and letting the loop grind down edge cases one at a time: nested lists, table backgrounds, flex layouts, blockquotes, the stuff that actually breaks HTML-to-docx converters in practice.&lt;/p&gt;

&lt;h2&gt;
  
  
  The results
&lt;/h2&gt;

&lt;p&gt;I benchmarked dom-docx against a couple of the established OSS HTML-to-docx converters on npm, solid, widely-used pure-JS libraries with no headless browser requirement, using the same scoring harness across 37 real-world test cases.&lt;/p&gt;

&lt;p&gt;A few things stood out once the numbers came in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;dom-docx produced schema-valid OOXML on all 37 cases in this harness, which held up as the clearest gap.&lt;/li&gt;
&lt;li&gt;Average layout-fidelity score in the mid-90s%, versus the mid-60s% for the alternatives tested.&lt;/li&gt;
&lt;li&gt;The biggest gaps show up exactly where you'd expect: nested lists inside blockquotes, table row backgrounds, flex layouts, inline SVG. Simple stuff (a plain paragraph, a centered paragraph) is roughly a wash across the board, which makes sense since that's the easy 20% every converter gets right.&lt;/li&gt;
&lt;li&gt;dom-docx trades some raw compile speed for it (~50ms avg vs the high-teens ms range), worth knowing if you're converting at very high volume, though for most document-generation use cases that's not the bottleneck.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Full per-case breakdown, named comparisons and methodology are public if you want the receipts: &lt;a href="https://github.com/floodtide/dom-docx/blob/main/docs/BENCHMARK.md" rel="noopener noreferrer"&gt;BENCHMARK.md&lt;/a&gt; and &lt;a href="https://github.com/floodtide/dom-docx/blob/main/docs/SCORING.md" rel="noopener noreferrer"&gt;SCORING.md&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;dom-docx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Requires Node ≥ 20. The default path is pure JS, no browser or Playwright needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Node:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;writeFile&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;node:fs/promises&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;convertHtmlToDocx&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dom-docx&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;html&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`
&amp;lt;h1 style="color:#1a1a2e"&amp;gt;Quarterly Report&amp;lt;/h1&amp;gt;
&amp;lt;p&amp;gt;Revenue grew &amp;lt;strong&amp;gt;12%&amp;lt;/strong&amp;gt; year over year.&amp;lt;/p&amp;gt;
&amp;lt;ul&amp;gt;
  &amp;lt;li&amp;gt;North America&amp;lt;/li&amp;gt;
  &amp;lt;li&amp;gt;EMEA&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;docx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;convertHtmlToDocx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;writeFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;output.docx&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;docx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Browser&lt;/strong&gt; (runs entirely client-side, no Playwright):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;convertHtmlToDocx&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dom-docx/browser&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;blob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;convertHtmlToDocx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createObjectURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;download&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;output.docx&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;CLI&lt;/strong&gt;, no code required:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx dom-docx input.html &lt;span class="nt"&gt;-o&lt;/span&gt; output.docx
&lt;span class="nb"&gt;cat &lt;/span&gt;fragment.html | npx dom-docx - &lt;span class="nt"&gt;-o&lt;/span&gt; -   &lt;span class="c"&gt;# stdin to binary stdout, for pipelines&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's also a live demo at &lt;a href="https://dom-docx.com/" rel="noopener noreferrer"&gt;dom-docx.com&lt;/a&gt; if you want to test HTML with it in the browser first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where this is headed
&lt;/h2&gt;

&lt;p&gt;A growing share of the HTML hitting a converter like this is LLM-generated: agents building reports, RAG pipelines writing summaries and shipping them as Word docs. I added an &lt;a href="https://github.com/floodtide/dom-docx/blob/main/AGENTS.md" rel="noopener noreferrer"&gt;AGENTS.md&lt;/a&gt; that documents which HTML patterns convert cleanly and which to avoid, so an agent authoring the HTML can get it right the first time instead of producing DOCX-breaking markup and finding out after the fact.&lt;/p&gt;

&lt;p&gt;Repo's here: &lt;a href="https://github.com/floodtide/dom-docx" rel="noopener noreferrer"&gt;github.com/floodtide/dom-docx&lt;/a&gt;. MIT licensed, stars, issues and PRs welcome. I'd genuinely like to hear where it breaks for you.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>npm</category>
      <category>node</category>
      <category>browser</category>
    </item>
  </channel>
</rss>
