Export HTML to MS Word Document using JavaScript

Generally, the export feature is used to download web page content as a file and save it for offline use. Microsoft Word or Doc (.doc) format is ideal for exporting HTML content in a file. The export to doc functionality can be easily implemented on the web application without server-side interaction. There is a client-side solution to export HTML to word document using JavaScript.

The client-side export to doc functionality makes the web application user-friendly. The user can export a specific part of the web page content without page refresh. In this tutorial, we will show you how to export HTML to doc using JavaScript. The JavaScript export functionality can be used to download web page content or specific div content in a doc/docx file.

Export HTML to MS Word Document

The example code converts the HTML content to a Microsoft Word document and it can be saved as a .doc file.

JavaScript Code:
The Export2Word() function converts HTML content to word or export specific part of a web page with images, and download as Doc file (.doc).

  • element – Required. Specify the element ID to export content from.
  • filename – Optional. Specify the file name of the word document.
function Export2Word(element, filename = ''){
    var preHtml = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'><head><meta charset='utf-8'><title>Export HTML To Doc</title></head><body>";
    var postHtml = "</body></html>";
    var html = preHtml+document.getElementById(element).innerHTML+postHtml;

    var blob = new Blob(['\ufeff', html], {
        type: 'application/msword'
    });
    
    // Specify link url
    var url = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(html);
    
    // Specify file name
    filename = filename?filename+'.doc':'document.doc';
    
    // Create download link element
    var downloadLink = document.createElement("a");

    document.body.appendChild(downloadLink);
    
    if(navigator.msSaveOrOpenBlob ){
        navigator.msSaveOrOpenBlob(blob, filename);
    }else{
        // Create a link to the file
        downloadLink.href = url;
        
        // Setting the file name
        downloadLink.download = filename;
        
        //triggering the function
        downloadLink.click();
    }
    
    document.body.removeChild(downloadLink);
}

Export HTML Table Data to Excel using JavaScript

HTML Content:
Wrap the HTML content in a container you want to export to MS Word document (.doc).

<div id="exportContent">
    <!-- Your content here -->
</div>

Trigger Export2Word() function to export HTML content as .doc file using JavaScript.

<button onclick="Export2Word('exportContent');">Export as .doc</button>

If you want to export HTML with a custom file name, pass your desired file name in the Export2Word() function.

<button onclick="Export2Word('exportContent', 'word-content');">Export as .doc</button>

By default, the word file will be saved as a .doc file. If you want to export word file as a .docx file, specify the extension in the file name.

<button onclick="Export2Word('exportContent', 'word-content.docx');">Export as .docx</button>

Export TinyMCE Content to MS Word Document

Conclusion

Our example code helps you to integrate export to doc functionality using JavaScript without any third-party jQuery plugin. Not only .doc but also you can export HTML content as a .docx file by specifying the extension. Also, you can easily extend the functionality of export to word script as per your needs.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

20 Comments

  1. Prince Said...
  2. Sachin Said...
  3. Entropy Said...
  4. Sagar Gopale Said...
  5. Amita Said...
  6. Smith Said...
  7. LOREN VANDERHOFF Said...
  8. LOREN VANDERHOFF Said...
  9. V. Sangeetha Said...
  10. Jesus Bello Said...
  11. Bimla Said...
  12. Chris Said...
  13. CMR Said...
  14. Mahesh Said...
  15. Dinarte Said...
  16. Olena Pedush Said...
  17. Rocky Said...
  18. Cj Said...
  19. Vishal Dhawan Said...
    • CodexWorld Said...

Leave a reply

keyboard_double_arrow_up