Last active
April 22, 2022 03:08
-
-
Save naagaraa/e8b8a12b85f018a3ac291cc558e1a1e4 to your computer and use it in GitHub Desktop.
html to word
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function exportHTML(){ | |
var vm = this, word = `<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 Word Document with JavaScript</title></head><body>${vm.word}</body></html>`; | |
var source = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(word); | |
var fileDownload = document.createElement("a"); | |
document.body.appendChild(fileDownload); | |
fileDownload.href = source; | |
fileDownload.download = 'hsl-file.doc'; | |
fileDownload.click(); | |
document.body.removeChild(fileDownload); | |
} | |
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(do |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment