docx vs html-docx-js vs mammoth
Generating and Converting Word Documents in JavaScript
docxhtml-docx-jsmammothSimilar Packages:

Generating and Converting Word Documents in JavaScript

docx, html-docx-js, and mammoth are essential tools for handling Microsoft Word documents in JavaScript environments, but they serve distinct directions in the data flow. docx is a modern library designed for programmatically creating .docx files from scratch using JavaScript objects. html-docx-js focuses on converting HTML content directly into Word format, acting as a bridge between web views and documents. mammoth takes the opposite approach, specializing in converting existing .docx files into HTML or plain text for display in web applications. Together, they cover the full lifecycle of document handling — creation, HTML-to-Word conversion, and Word-to-HTML extraction.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
docx05,6783.56 MB1592 months agoMIT
html-docx-js01,149-8310 years agoMIT
mammoth06,1932.17 MB622 months agoBSD-2-Clause

docx vs html-docx-js vs mammoth: Architecture and Use Cases

When working with Microsoft Word files in JavaScript, developers often face confusion about which library handles which part of the pipeline. docx, html-docx-js, and mammoth are not direct competitors — they are specialized tools for different directions of data flow. Understanding their specific roles is critical for architectural decisions.

🔄 Data Flow Direction: Create vs Convert vs Extract

The most important distinction is the direction of conversion. docx builds files from JavaScript objects. html-docx-js wraps HTML into a Word file. mammoth unwraps Word files into HTML.

docx creates documents from scratch using a structured API.

// docx: Creating a document from JS objects
const { Document, Packer, Paragraph } = require("docx");

const doc = new Document({
  sections: [{
    properties: {},
    children: [
      new Paragraph({ text: "Hello World" })
    ]
  }]
});

Packer.toBuffer(doc).then(buffer => {
  // Save buffer to file
});

html-docx-js converts an HTML string directly into a buffer.

// html-docx-js: Converting HTML to Docx
const htmlDocx = require('html-docx-js');
const fs = require('fs');

const htmlContent = '<h1>Hello World</h1>';
const converted = htmlDocx.asBuffer(htmlContent);

fs.writeFileSync('document.docx', converted);

mammoth extracts HTML from an existing .docx file.

// mammoth: Converting Docx to HTML
const mammoth = require("mammoth");

mammoth.convertToHtml({ path: "document.docx" })
  .then(result => {
    const html = result.value; // The generated HTML
    console.log(html);
  });

🎨 Styling Control: Programmatic vs CSS vs Mapping

How you control the look of the document varies wildly between these tools. docx requires defining styles in JavaScript. html-docx-js relies on inline CSS. mammoth uses a style map to translate Word styles to HTML.

docx uses a dedicated styling system within the library.

// docx: Defining styles programmatically
const { HeadingLevel } = require("docx");

const doc = new Document({
  sections: [{
    children: [
      new Paragraph({
        text: "Title",
        heading: HeadingLevel.HEADING_1
      })
    ]
  }]
});

html-docx-js uses standard HTML CSS for styling.

// html-docx-js: Styling via CSS
const html = `
  <h1 style="color: blue; font-size: 20px;">Title</h1>
  <p style="font-family: Arial;">Content</p>
`;
const converted = htmlDocx.asBuffer(html);

mammoth maps Word styles to HTML classes or tags.

// mammoth: Custom style mapping
const options = {
  styleMap: [
    "p[style-name='Heading 1'] => h1:fresh",
    "p[style-name='Normal'] => p"
  ]
};

mammoth.convertToHtml({ path: "input.docx" }, options);

🖼️ Image Handling: Buffers vs Base64 vs Extraction

Images are a common requirement. Each library handles image data differently based on its input source.

docx requires image buffers or data URIs defined in the object tree.

// docx: Adding images from buffer
const { ImageRun } = require("docx");
const fs = require("fs");

const imageBuffer = fs.readFileSync("image.png");

const doc = new Document({
  sections: [{
    children: [
      new Paragraph({
        children: [
          new ImageRun({
            data: imageBuffer,
            transformation: { width: 100, height: 100 }
          })
        ]
      })
    ]
  }]
});

html-docx-js supports standard HTML image tags with Base64 sources.

// html-docx-js: Images via HTML tags
const html = `
  <img src="data:image/png;base64,iVBORw0KGgo..." />
`;
const converted = htmlDocx.asBuffer(html);

mammoth extracts images and can return them as separate files or Base64.

// mammoth: Extracting images
const options = {
  convertImage: mammoth.images.imgElement(function(image) {
    return image.read("base64").then(function(imageBuffer) {
      return {
        src: "data:" + image.contentType + ";base64," + imageBuffer
      };
    });
  })
};

mammoth.convertToHtml({ path: "input.docx" }, options);

🛠️ Maintenance and Ecosystem Health

When selecting a library for long-term projects, maintenance status is a key factor. docx and mammoth are actively maintained with regular updates. html-docx-js has seen less activity in recent years.

docx is the current industry standard for generation.

  • Active development on GitHub.
  • Supports modern Word features like content controls and complex tables.
  • Regularly updated for bug fixes and TypeScript support.

mammoth is stable and reliable for extraction.

  • Focused scope leads to high stability.
  • Widely used in content management systems.
  • Good documentation for style mapping.

html-docx-js is considered legacy by many architects.

  • Last major updates were several years ago.
  • Many teams now prefer generating HTML and using docx for better control, or use forks like html-docx-js-typescript.
  • Suitable for simple exports but risky for complex enterprise needs.

📊 Summary: Capabilities Matrix

Featuredocxhtml-docx-jsmammoth
Primary GoalCreate .docx from JSConvert HTML to .docxConvert .docx to HTML
Input FormatJavaScript ObjectsHTML String.docx File
Output Format.docx Buffer.docx BufferHTML String
StylingJS Object APICSSStyle Mapping
Image SupportBuffers / URIsBase64 / URLsExtraction to Base64
Maintenance✅ Active⚠️ Low Activity✅ Active

💡 The Big Picture

These libraries are best viewed as parts of a complete document system rather than alternatives to each other.

docx is your authoring engine. Use it when you own the data and need to build a document dynamically. It gives you the most power but requires the most code.

html-docx-js is your quick exporter. Use it when you have a web view and need to let users download it as a Word file. It is fast to implement but less robust.

mammoth is your importer. Use it when users upload Word files and you need to display or process that content on the web. It ensures clean HTML output without legacy Word markup.

Final Thought: For modern applications, a common pattern is using mammoth to import legacy content, storing it as HTML in your database, and using docx to regenerate professional reports from that data when needed. Avoid relying solely on html-docx-js for critical document generation if you require strict formatting control.

How to Choose: docx vs html-docx-js vs mammoth

  • docx:

    Choose docx when you need to generate complex Word documents programmatically from data sources like databases or APIs. It offers the most control over document structure, styles, and elements like tables or charts. This is the best fit for reporting engines, invoice generators, or any scenario where the document structure is defined in code rather than existing templates.

  • html-docx-js:

    Choose html-docx-js if your content already exists as HTML and you need a quick conversion to Word format without rebuilding the layout in JavaScript objects. It is suitable for features like 'Export to Word' buttons on web pages where the visual fidelity of the HTML is the priority. Note that this package is older and may lack support for newer Word features compared to docx.

  • mammoth:

    Choose mammoth when you need to import or display content from existing Word documents within a web application. It is ideal for content management systems, document viewers, or migration tools where the goal is to extract clean HTML from .docx files. It does not create documents, so it is not suitable for generation tasks.

README for docx

clippy the assistant

Easily generate and modify .docx files with JS/TS. Works for Node and on the Browser.


NPM version Downloads per month GitHub Action Workflow Status Known Vulnerabilities PRs Welcome codecov Docx.js Editor

drawing

Demo

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:

Node

Press endpoint on the RunKit website:

RunKit Instructions

More here

How to use & Documentation

Please refer to the documentation at https://docx.js.org/ for details on how to use this library, examples and much more!

Playground

Experience docx in action through Docx.js Editor, an interactive playground where you can code and preview the results in real-time.

Examples

Check the demo folder for examples.

Contributing

Read the contribution guidelines here.

Used by

drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing

...and many more!


patreon browserstack

Made with 💖