html-docx-js and html-to-docx are both JavaScript libraries designed to convert HTML content into Microsoft Word (.docx) documents. They enable developers to generate rich-text reports, invoices, or letters directly from web technologies without requiring server-side Office installations. While they share the same core goal, they differ significantly in architecture, maintenance status, and feature support, making the choice critical for long-term project stability.
Generating Word documents from HTML is a common requirement in enterprise web applications — think invoices, reports, or exportable content. Two popular npm packages tackle this problem: html-docx-js and html-to-docx. While they sound similar, they are not interchangeable. One is a legacy library that has stalled, while the other is the modern standard. Let's dive into the technical realities.
html-docx-js is effectively archived.
// html-docx-js: Basic usage (legacy)
const htmlDocx = require('html-docx-js');
const converted = htmlDocx.asBlob('<h1>Hello World</h1>');
// No support for advanced options like headers/footers in recent versions
html-to-docx is actively maintained.
// html-to-docx: Basic usage (modern)
const HTMLtoDOCX = require('html-to-docx');
const fileBuffer = await HTMLtoDOCX('<h1>Hello World</h1>', null, {
table: { row: { cantSplit: true } }
});
// Supports extensive configuration options
html-docx-js has limited CSS parsing.
<b>, <i>, and <p>.// html-docx-js: Styling limitations
const html = '<div style="display: flex; color: #333;">Content</div>';
const blob = htmlDocx.asBlob(html);
// Flexbox will likely be ignored; output may look broken
html-to-docx supports a wider range of CSS.
// html-to-docx: Better styling support
const html = '<table style="border-collapse: collapse;"><tr><td>Cell</td></tr></table>';
const buffer = await HTMLtoDOCX(html, null, {
footer: '<p>Page {PAGE}</p>'
});
// Tables and borders render correctly in most Word versions
html-docx-js lacks built-in support for complex document structures.
// html-docx-js: No native header/footer API
// Developers often have to modify the generated blob manually
// which is error-prone and brittle
const blob = htmlDocx.asBlob(content);
// No config option for headers/footers
html-to-docx provides explicit configuration for document parts.
// html-to-docx: Native header/footer support
const buffer = await HTMLtoDOCX(html, null, {
header: '<p>Company Confidential</p>',
footer: '<p>Page {PAGE} of {NUMPAGES}</p>',
orientation: 'landscape'
});
// Structured document parts are handled automatically
html-docx-js was originally built with Node.js in mind.
Buffer and Blob.// html-docx-js: Browser bundling issues
// May require webpack config to handle Node core modules
import htmlDocx from 'html-docx-js';
// Often needs global.Buffer polyfill
html-to-docx is designed for universal usage.
// html-to-docx: Universal compatibility
import HTMLtoDOCX from 'html-to-docx';
// Works in browser without extra Buffer polyfills
const blob = await HTMLtoDOCX(html, null, { fileType: 'blob' });
Despite the differences, both libraries aim to solve the same problem using similar underlying concepts.
// Both accept HTML strings
const html = '<h1>Title</h1><p>Content</p>';
// html-docx-js
htmlDocx.asBlob(html);
// html-to-docx
HTMLtoDOCX(html);
.docx files compatible with Microsoft Word.// Both produce Word-compatible files
// html-docx-js returns a Blob by default
// html-to-docx can return Buffer or Blob via options
// Both abstract WordML complexity
// You write HTML, they generate XML inside the .docx zip
| Feature | html-docx-js | html-to-docx |
|---|---|---|
| Maintenance | ❌ Archived / Stalled | ✅ Active / Regular Updates |
| CSS Support | ⚠️ Basic / Limited | ✅ Extensive / Modern |
| Headers/Footers | ❌ Not Supported Natively | ✅ Configurable Options |
| Image Handling | ⚠️ Base64 Only / Unreliable | ✅ URLs, Buffers, Base64 |
| Browser Ready | ⚠️ Requires Polyfills | ✅ Out-of-the-Box |
| Page Orientation | ❌ Fixed | ✅ Configurable |
html-docx-js is a legacy tool 🕰️. It might work for simple, internal tools where formatting doesn't matter and you're stuck on an old stack. However, using it in 2024+ introduces technical debt. It lacks the features modern users expect (like proper headers, footers, and CSS fidelity) and carries the risk of unmaintained dependencies.
html-to-docx is the production-ready choice 🏭. It handles the edge cases that actually matter in business applications — page breaks, branding in headers, and consistent table rendering. The API is slightly more verbose but offers the control you need for professional documents. It works everywhere (Node and browser) without fighting your bundler.
Final Thought: If you are starting a new feature or project, do not use html-docx-js. The time saved initially will be lost debugging formatting issues later. html-to-docx is the clear architectural choice for stability and quality.
Choose html-docx-js only if you are maintaining a legacy system that already depends on it and cannot migrate easily. It is effectively archived and no longer receives updates, meaning it lacks support for modern CSS features and may have unresolved security or compatibility issues. For any new development, this package should be avoided in favor of actively maintained alternatives.
Choose html-to-docx for all new projects requiring HTML-to-Word conversion. It is actively maintained, supports modern CSS styling, handles images and tables more reliably, and works seamlessly in both Node.js and browser environments. Its API is more robust, offering better control over document headers, footers, and page orientation, making it the standard choice for production applications.
This is a very small library that is capable of converting HTML documents to DOCX format that is used by Microsoft Word 2007 and onward. It manages to perform the conversion in the browser by using a feature called 'altchunks'. In a nutshell, it allows embedding content in a different markup language. We are using MHT document to ship the embedded content to Word as it allows to handle images. After Word opens such file, it converts the external content to Word Processing ML (this is how the markup language of DOCX files is called) and replaces the reference.
Altchunks were not supported by Microsoft Word for Mac 2008 and are not supported by LibreOffice and Google Docs.
This library should work on any modern browser that supports Blobs (either natively or via
Blob.js). It was tested on Google Chrome 36, Safari 7 and
Internet Explorer 10.
It also works on Node.js (tested on v0.10.12) using Buffer instead of Blob.
This library supports only inlined base64 images (sourced via DATA URI). But it is easy to convert a
regular image (sourced from static folder) on the fly. If you need an example of such conversion you can checkout a demo page source (see function convertImagesToBase64).
Very minimal demo is available as test/sample.html in the repository and
online. Please note that saving
files on Safari is a little bit convoluted and the only reliable method seems to be falling back
to a Flash-based approach (such as Downloadify).
Our demo does not include this workaround to keep things simple, so it will not work on Safari at
this point of time.
You can also find a sample for using it in Node.js environment here.
To generate DOCX, simply pass a HTML document (as string) to asBlob method to receive Blob (or Buffer)
containing the output file.
var converted = htmlDocx.asBlob(content);
saveAs(converted, 'test.docx');
asBlob can take additional options for controlling page setup for the document:
orientation: landscape or portrait (default)margins: map of margin sizes (expressed in twentieths of point, see
WordprocessingML documentation for details):
top: number (default: 1440, i.e. 2.54 cm)right: number (default: 1440)bottom: number (default: 1440)left: number (default: 1440)header: number (default: 720)footer: number (default: 720)gutter: number (default: 0)For example:
var converted = htmlDocx.asBlob(content, {orientation: 'landscape', margins: {top: 720}});
saveAs(converted, 'test.docx');
IMPORTANT: please pass a complete, valid HTML (including DOCTYPE, html and body tags).
This may be less convenient, but gives you possibility of including CSS rules in style tags.
html-docx-js is distributed as 'standalone' Browserify module (UMD). You can require it as
html-docx. If no module loader is available, it will register itself as window.htmlDocx.
See test/sample.html for details.
Copyright (c) 2015 Evidence Prime, Inc. See the LICENSE file for license rights and limitations (MIT).