html-docx-js vs html-to-docx
HTML to DOCX Conversion
html-docx-jshtml-to-docxSimilar Packages:

HTML to DOCX Conversion

HTML to DOCX conversion libraries allow developers to convert HTML content into DOCX (Microsoft Word) files programmatically. This is useful for generating documents from web applications, such as reports, invoices, or any content that needs to be exported in a Word-compatible format. These libraries typically parse the HTML, apply styles, and create a DOCX file that retains the structure and formatting of the original content. html-docx-js is a JavaScript library that converts HTML to DOCX files directly in the browser, while html-to-docx is a Node.js library that provides a simple API for converting HTML to DOCX files on the server side.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
html-docx-js01,148-8310 years agoMIT
html-to-docx04794.8 MB1063 years agoMIT

Feature Comparison: html-docx-js vs html-to-docx

Conversion Method

  • html-docx-js:

    html-docx-js converts HTML to DOCX directly in the browser using JavaScript. It does not require any server-side processing, making it a great choice for client-side applications.

  • html-to-docx:

    html-to-docx converts HTML to DOCX on the server side using Node.js. It is designed for backend applications and can handle larger HTML content more efficiently.

File Size and Performance

  • html-docx-js:

    html-docx-js is lightweight and performs well for small to medium-sized HTML content. However, performance may degrade with very large or complex HTML structures due to browser limitations.

  • html-to-docx:

    html-to-docx is more efficient for handling larger HTML files, as it runs on the server and is not limited by browser memory and processing power.

Styling and Formatting

  • html-docx-js:

    html-docx-js supports basic HTML and CSS styling, but complex styles may not be rendered accurately. It is best suited for simple to moderately styled content.

  • html-to-docx:

    html-to-docx provides better support for advanced styling and can handle more complex HTML structures, making it a better choice for documents that require precise formatting.

Dependencies

  • html-docx-js:

    html-docx-js has no external dependencies, making it easy to integrate into any web application.

  • html-to-docx:

    html-to-docx is a Node.js package and requires a server environment. It may have some dependencies related to HTML parsing and DOCX generation.

Ease of Use: Code Examples

  • html-docx-js:

    html-docx-js is easy to use with a simple API for converting HTML to DOCX. Here’s a quick example:

    const htmlDocx = require('html-docx-js');
    const html = '<h1>Hello World</h1>'; // Your HTML content
    const docx = htmlDocx.asBlob(html);
    const link = document.createElement('a');
    link.href = URL.createObjectURL(docx);
    link.download = 'document.docx';
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    
  • html-to-docx:

    html-to-docx provides a straightforward API for server-side HTML to DOCX conversion. Example:

    const { htmlToDocx } = require('html-to-docx');
    const fs = require('fs');
    const html = '<h1>Hello World</h1>'; // Your HTML content
    const docxBuffer = await htmlToDocx(html, { margin: '1in' });
    fs.writeFileSync('document.docx', docxBuffer);
    

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

  • html-docx-js:

    Choose html-docx-js if you need a client-side solution that works entirely in the browser without any server-side processing. It is ideal for applications where you want to generate DOCX files directly from user input or dynamic HTML content on the client side.

  • html-to-docx:

    Choose html-to-docx if you are working on a Node.js application and need a server-side solution for converting HTML to DOCX. It is suitable for generating documents from server-side rendered content or when you need to handle larger HTML files and perform more complex processing.

README for html-docx-js

html-docx-js

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.

Compatibility

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.

Images Support

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).

Usage and demo

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.

License

Copyright (c) 2015 Evidence Prime, Inc. See the LICENSE file for license rights and limitations (MIT).