[英]Html to MS Word using JS without Header and Footer
I am trying to convert HTML page into MS Word document file with help of below code.我正在尝试借助以下代码将 HTML 页面转换为 MS Word 文档文件。 My problem is I don't want any header and footer in it.
我的问题是我不想要任何页眉和页脚。 I want to create word file without header and footer extra spaces.
我想创建没有页眉和页脚额外空格的 word 文件。 Content should be sticky to the top.
内容应该粘在顶部。
I am getting the results but I don't want header and footer in it.我得到了结果,但我不想在其中添加页眉和页脚。
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><style>body,html{margin-top: 0px !important;padding-top: 0px !important;padding-left: 0px !important;margin-left: 0px !important;font-size: 12px;margin-left: 0px !important;}.scheduledtxt{font-size: 14.5px;}table{margin: auto 0px !important;}</head><body>'; var postHtml="</body></html>"; var html = preHtml+document.getElementById(element).innerHTML+postHtml.trim(); var blob = new Blob(['\', html], { type: 'application/msword' }); // Specify link url var url = 'data:application/msword;charset=utf-8,' + encodeURIComponent(html); // Specify file name filename = "test.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); }
<span id="exportContent"> <h2 style="text-align: center;">Test Document</h2> </span> <div style="text-align: center;"> <button id="hiddenBtn" style="background: royalblue; color: white;padding: 10px; border-radius: 10px;" onclick="Export2Doc('exportContent', 'word-content');">Export as .doc</button> </div>
I have manage to achieve my goal with the following link css print mode: display header and footer only on first page of a generated word doc我设法通过以下链接css 打印模式实现了我的目标:仅在生成的 word 文档的第一页上显示页眉和页脚
I just had to add below css code.我只需要添加下面的 css 代码。 for better understanding just minify entire code and use when converting to the word file.
为了更好地理解,只需缩小整个代码并在转换为 word 文件时使用。
@page {
size: 2cm 2.7cmt;
margin: 0.4cm 0cm 0cm 0cm;
mso-page-orientation: portrait;
mso-header: none;
mso-footer: none;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.