docx, html-docx-js, and html-to-docx are JavaScript libraries used to create Microsoft Word (.docx) files. docx is a programmatic builder that lets you construct documents element-by-element using JavaScript objects. html-docx-js and html-to-docx focus on converting existing HTML content into Word format. While docx offers fine-grained control over layout and styling, the HTML converters prioritize speed and ease of use when migrating web content to documents.
Creating Word documents in JavaScript is a common requirement for reporting, exporting data, or archiving web content. The three main contenders — docx, html-docx-js, and html-to-docx — take very different approaches. docx builds files from scratch using code, while the other two convert HTML strings into documents. Let's look at how they handle real-world engineering tasks.
docx requires you to define every part of the document using JavaScript objects.
// docx: Programmatic creation
const { Document, Packer, Paragraph, TextRun } = require("docx");
const doc = new Document({
sections: [{
properties: {},
children: [
new Paragraph({
children: [new TextRun("Hello World")],
}),
],
}],
});
Packer.toBuffer(doc).then((buffer) => {
// Save buffer to file
});
html-docx-js takes an HTML string and converts it directly.
// html-docx-js: HTML conversion
const htmlDocx = require("html-docx-js");
const htmlContent = `<!DOCTYPE html><html><body><h1>Hello World</h1></body></html>`;
const converted = htmlDocx.asBlob(htmlContent);
// Returns a Blob object ready for download or saving
html-to-docx also converts HTML but supports more modern options.
html-docx-js.// html-to-docx: HTML conversion with options
const HTMLtoDOCX = require("html-to-docx");
const htmlContent = `<h1>Hello World</h1>`;
HTMLtoDOCX(htmlContent, null, {
table: { row: { cantSplit: true } },
footer: true,
}).then((fileBuffer) => {
// Save buffer to file
});
docx uses its own styling system that maps to Word's internal XML.
styles array and reference them by ID.// docx: Custom styles
const doc = new Document({
styles: {
paragraphStyles: [{
id: "heading1",
name: "Heading 1",
basedOn: "Normal",
quickFormat: true,
run: { size: 28, bold: true },
}],
},
sections: [{
children: [
new Paragraph({
text: "Title",
heading: "Heading1",
}),
],
}],
});
html-docx-js relies on inline CSS or style tags.
// html-docx-js: Inline CSS
const html = `<h1 style="color: blue; font-size: 24pt;">Title</h1>`;
const converted = htmlDocx.asBlob(html);
// Advanced CSS may be ignored or rendered incorrectly
html-to-docx supports a wider range of CSS properties.
// html-to-docx: CSS mapping
const html = `<h1 style="color: blue; font-size: 24pt;">Title</h1>`;
HTMLtoDOCX(html).then((buffer) => {
// Better CSS fidelity than html-docx-js
});
docx requires images to be added as media elements with explicit dimensions.
// docx: Image insertion
const doc = new Document({
sections: [{
children: [
new Paragraph({
children: [
new ImageRun({
data: fs.readFileSync("image.png"),
transformation: { width: 100, height: 100 },
}),
],
}),
],
}],
});
html-docx-js supports images via standard <img> tags.
// html-docx-js: Image tag
const html = `<img src="data:image/png;base64,..." width="100" />`;
const converted = htmlDocx.asBlob(html);
// External URLs often blocked by security policies
html-to-docx also uses <img> tags but handles fetching better.
// html-to-docx: Image fetching
const html = `<img src="https://example.com/image.png" />`;
HTMLtoDOCX(html, null, {
converter: {
image: { url: "https://example.com/image.png" }
}
}).then((buffer) => {
// Handles external resources more gracefully
});
docx is actively maintained with frequent updates.
// docx: Active development
// npm install docx
// Regularly updated with new features like content controls
html-docx-js is largely stagnant with minimal updates.
// html-docx-js: Legacy status
// npm install html-docx-js
// Last major updates were years ago; use with caution
html-to-docx is actively maintained as a modern alternative.
html-docx-js.// html-to-docx: Modern alternative
// npm install html-to-docx
// Actively maintained with better CSS support
docx works in both Node.js and browsers.
// docx: Browser support
Packer.toBlob(doc).then((blob) => {
saveAs(blob, "document.docx"); // Using file-saver or similar
});
html-docx-js was originally built for browsers but works in Node.
// html-docx-js: Environment
const converted = htmlDocx.asBlob(html);
// May require buffer conversion in Node.js environments
html-to-docx is designed with Node.js in mind but supports browsers.
// html-to-docx: Node.js focus
HTMLtoDOCX(html).then((buffer) => {
fs.writeFileSync("document.docx", buffer);
});
| Feature | docx | html-docx-js | html-to-docx |
|---|---|---|---|
| Approach | Programmatic Builder | HTML Converter | HTML Converter |
| Control | High (Pixel-perfect) | Low (HTML dependent) | Medium (CSS mapped) |
| Maintenance | ✅ Active | ❌ Legacy/Stagnant | ✅ Active |
| Learning Curve | Steep (API heavy) | Low (HTML knowledge) | Low (HTML knowledge) |
| Best For | Reports, Invoices | Quick HTML exports | Rich HTML content |
docx is the professional choice for generating structured documents.
html-docx-js should be avoided in new projects.
html-to-docx is the right tool for content export.
Final Thought: Don't try to force HTML converters to do programmatic layout work. If you need tables, headers, and footers to align perfectly, invest the time in docx. If you just need to save a blog post, html-to-docx will save you days of development time.
Choose docx if you need precise control over the document structure, such as custom headers, footers, tables, or complex styling. It is the best choice for generating reports, invoices, or templates where consistency and layout matter more than quick HTML conversion. It is actively maintained and works in both Node.js and browsers.
Avoid html-docx-js for new projects. It is largely considered legacy software with limited maintenance and known issues with modern CSS support. Only use it if you are maintaining an older codebase that already depends on it and migration is not feasible.
Choose html-to-docx if your primary goal is to convert rich HTML content (like blog posts or emails) into Word documents quickly. It handles CSS styling better than html-docx-js and is more actively maintained. It is ideal when you already have HTML content and do not need to build the document structure manually.
Easily generate and modify .docx files with JS/TS. Works for Node and on the Browser.
Here are examples of docx being used with basic HTML/JS in a browser environment:
Here are examples of docx working in Angular:
Here are examples of docx working in React:
Here is an example of docx working in Vue.js:
Press endpoint on the RunKit website:

More here
Please refer to the documentation at https://docx.js.org/ for details on how to use this library, examples and much more!
Experience docx in action through Docx.js Editor, an interactive playground where you can code and preview the results in real-time.
Check the demo folder for examples.
Read the contribution guidelines here.
...and many more!
Made with 💖