简体   繁体   English

使用没有页眉和页脚的 JS 将 Html 转换为 MS Word

[英]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.

相关问题 如何使用OfficeJS API在MS Word中重新加载页眉和页脚 - How to reload header and footer in MS Word using officejs api 使用HTML将页眉和页脚包含为单独的文件 - Include Header and Footer as Separate Files Using HTML 使用js在多个页面上的页眉和页脚 - header and footer on multiple pages using js 使用printThis.js重复页眉和页脚 - repeat header and footer using printThis.js 如何使用 Node js、ExpressJS、hbs 或 ejs 在多页网站的 html 页面中添加通用页眉和页脚 - How add common header and footer in html page for multi page website using Node js, ExpressJS,hbs or ejs 使用Java将HTML页面转换为MS Word - Convert HTML page into MS word using java 具有固定页眉和页脚以及没有固定宽度的可滚动正文的 HTML 表格 - HTML table with fixed header and footer and scrollable body without fixed widths 打印预览不带应用程序页眉和页脚的html表 - Print preview a html table without header and footer of application 使用ejs模板引擎时,我是否可以在Node.js的每个页面中包含HTML代码块(如页脚和页眉)? - Can I include HTML blocks of code like footer and header on every page in Node.js whilst using ejs templating engine? MS文字样式的JS / HTML下拉菜单(帖子中的图片) - JS/HTML dropdown menu in MS word style(pic in the post)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM