<script> function exportHTML(){ var header = "<html xmlns:o='urn:schemas-microsoft-com:office:office' "+ "xmlns:w='urn:schemas-microsoft-com:office:word' "+ "xmlns='https://www.w3.org/TR/REC-html40'>"+ "<head><meta charset='utf-8'><title>Export HTML to Word Document with JavaScript</title></head><body>"; var footer = "</body></html>";and the html in the view is
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"); document.body.appendChild(fileDownload); fileDownload.href = source; fileDownload.download = 'document.doc'; fileDownload.click(); document.body.removeChild(fileDownload); } </script>
<div id="source-html"> content to print </div> <div class="content-footer"> <button id="btn-export" onclick="exportHTML();">Export to word doc</button> </div>My questions are: I can add a value to de div id, for example "source-html-[key]". Is there a way to include one script for each div in a view? Or a way to make the script work for any source-html-[whateverthisis] ID? I hope this makes any sense. Thanks for any help!
It can be dynamic like that if you insert the script into the body of the view just like that, but it can create some exhaustive HTML code if you have a bunch of entries.
Thanks Bobby!
I tried it but I must have done something wrong. Will retest. Thanks a lot.