Export HTML To Word Document With Hyperlink In JavaScript

Html & CSS
File Size: 1.09 KB
Views Total: 5598 views
Last Update:February 26, 2021
Publish Date: December 8, 2019
Official Website: Go to website
License: MIT
Plugin Author:
Demo    Download

Export HTML to word document with javaScript can be done in different ways. JavaScript for implementing this in client side. When, the HTML file is simple include hyperlinks, then it is a simple thing to export the HTML content to a word document. I doesn’t use any third-party jQuery libraries, but with just simple JavaScript. please check with demo and download on your system.


Table of Contents

How to use it:

1. Include the HTML code with id name.

<div id="source-html">
        <center>Artificial Intelligence</center>
        Artificial Intelligence(AI) is an emerging technology
        demonstrating machine intelligence. The sub studies like <u><i>Neural
                Networks</i>, <i>Robatics</i> or <i>Machine Learning</i></u> are
        the parts of AI. This technology is expected to be a prime part
        of the real world in all levels.

<div class="content-footer">
    <button id="btn-export" onclick="exportHTML();">Export to
        word doc</button>

2.  add java script function

    function exportHTML(){
       var header = "<html xmlns:o='urn:schemas-microsoft-com:office:office' "+
            "xmlns:w='urn:schemas-microsoft-com:office:word' "+
            "<head><meta charset='utf-8'><title>Export HTML to Word Document with JavaScript</title></head><body>";
       var footer = "</body></html>";
       var sourceHTML = header+document.getElementById("source-html").innerHTML+footer;
       var source = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(sourceHTML);
       var fileDownload = document.createElement("a");
       fileDownload.href = source;
       fileDownload.download = 'document.doc';


Mady Schaferhoff is an expert jQuery script at jQuerypost. He offers web development services to clients globally. He also loves sharing Html,CSS, jQuery knowledge through blog posts like this one.

Related jQuery Plugins