如何在JavaScript中将HTML页面导出到doc / docx文件时将页面大小自定义为A4和行距

时间:2019-05-23 04:14:47

标签: javascript export doc

我有js函数,可将HTML页面导出到doc文件。该功能效果很好,但是与原始html页面相比,在导出的doc文件中会出现额外的行距,而不是A4大小的页面。我需要将文档文件页面大小设置为A4,并删除多余的行间距。有人可以帮我吗?

我研究了有关将HTML导出到doc / docx的问题,并尝试了一些js代码,该代码转换得很好,但CSS页面大小代码无法正常工作。

//Here is my html code:
 <div id="doc_block">
// in this div content appends 
   <div id="view_delivered_articles_modal_quill_editor" class="WordSection1" style="display: none;"></div>
 </div>

//这是js代码:

$(document).on('click', '.download_link_quill_editor', function(){      
        Export2Doc();
 });



function Export2Doc(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>";


    if (!window.Blob) {
       alert('Your legacy browser does not support this action.');
       return;
     }

    var html, link, blob, url, css;

           // EU A4 use: size: 841.95pt 595.35pt;
           // US Letter use: size:11.0in 8.5in;

    css = (
            '<style>' +
            '@page WordSection1{size: 841.95pt 595.35pt;mso-page- 
             orientation: portrait;}' +
            'div.WordSection1 {page: WordSection1;}' +
            '</style>'
           );

     html = preHtml + window.doc_block.innerHTML + postHtml;
     blob = new Blob(['\ufeff', css + html], {
          type: 'application/msword'
     });

      url = URL.createObjectURL(blob);
      link = document.createElement('A');
      link.href = url;
    // Set default file name. 
    // Word will append file extension - do not add an extension here.
      link.download = 'Document';   
      document.body.appendChild(link);
      if (navigator.msSaveOrOpenBlob ) 
         navigator.msSaveOrOpenBlob( blob, 'Document.doc'); // IE10-11
      else link.click();  // other browsers
           document.body.removeChild(link);

   }

///我希望导出文档文件的大小应为A4,并删除多余的行距。

0 个答案:

没有答案