Skip to content

Instantly share code, notes, and snippets.

@miwebguy
Last active December 26, 2023 19:05
Show Gist options
  • Save miwebguy/6a5a82cb61bf4e61756e16c7de7336b8 to your computer and use it in GitHub Desktop.
Save miwebguy/6a5a82cb61bf4e61756e16c7de7336b8 to your computer and use it in GitHub Desktop.
JStoDoc (no Images)
https://stackoverflow.com/questions/63460485/exporting-html-to-word
<script>
//<![CDATA[
function Export2Doc(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);
}
//]]>
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment