【问题标题】:how to make .doc/.docx file from HTML (as string)?如何从 HTML(作为字符串)制作 .doc/.docx 文件?
【发布时间】:2011-12-18 17:07:03
【问题描述】:

我有一个像下面这样的html格式的srting

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><head><style><!--

                    /*paged media */ div.header {display: none }
                    div.footer {display: none } /*@media print { */


                    @page { size: A4; margin: 10%; @top-center {
                    content: element(header) } @bottom-center {
                    content: element(footer) } }


                    /*font definitions*/

                    /*element styles*/ .del
                    {text-decoration:line-through;color:red;}

                          .ins {text-decoration:none;background:#c0ffc0;padding:1px;}



                    /* Word style definitions */

 /* TABLE STYLES */ 

 /* PARAGRAPH STYLES */ 
.DocDefaults {display:block;space-after: 4mm;line-height: 115%;font-family: Calibri;font-size: 11.0pt;}
.Normal {display:block;}

 /* CHARACTER STYLES */ .DefaultParagraphFont {display:inline;}


                    /* TABLE CELL STYLES */
                    --></style><script type="text/javascript">

                function toggleDiv(divid){
                    if(document.getElementById(divid).style.display == 'none'){
                        document.getElementById(divid).style.display = 'block';
                    }else{
                        document.getElementById(divid).style.display = 'none';
                    }
                }

            </script></head><body>

  <!-- userBodyTop goes here -->



  <div class="document">

  <p class="Normal DocDefaults "><span style="font-weight: bold;">Hi</span><span style="white-space:pre-wrap;"> </span>[name]<span style="white-space:pre-wrap;">. </span><span style="font-weight: bold;color: #FF0000;">your</span><span style="white-space:pre-wrap;"> ac is</span><span style="white-space:pre-wrap;"> </span>[acc_no]<span style="white-space:pre-wrap;">, and </span><span style="font-weight: bold;color: #FF0000;">your</span><span style="white-space:pre-wrap;"> password </span><span style="white-space:pre-wrap;">is </span>[password].</p>

  <p class="Normal DocDefaults ">Thanks you.</p></div>







  <!-- userBodyTail goes here -->

  </body></html>

我想使用这个字符串制作一个 .doc 或 .docx 文件。 doc或docx应该保存在目录和数据库中的链接以供下载。怎么办?

【问题讨论】:

  • 你有没有尝试过?你也见过phpdocx.com吗?
  • phpdocx 需要服务器中的 pdf 和 zip 模块。但我无法安装这些模块。所以我正在寻找核心 php 代码。上面的字符串可以导出为文档,用户可以保存。但我的要求是将其保存在服务器目录中并将目录链接到数据库以供以后下载
  • 当我在浏览器中显示 html(文档另存为网页)时,文档的样式会正确显示。当我再次将其导出到 doc 时,样式显示不正确。有什么问题?

标签: php html docx doc


【解决方案1】:
/* HTML to Microsoft Word Export
* This code demonstrates how to export an html element to Microsoft Word
* with CSS styles to set page orientation and paper size.
* Tested with Word 2010, 2013 and FireFox, Chrome, Opera, IE10-11
* Fails in legacy browsers (IE<10) that lack window.Blob object
*/
function saveDoc() {

  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;}\
   h1 {font-family: "Times New Roman", Georgia, Serif; font-size: 16pt;}\
   p {font-family: "Times New Roman", Georgia, Serif; font-size: 14pt;}\
   </style>\
  ');

  var rightAligned = document.getElementsByClassName("sm-align-right");
  for (var i=0, max=rightAligned.length; i < max; i++) {
    rightAligned[i].style = "text-align: right;"
  }

  var centerAligned = document.getElementsByClassName("sm-align-center");
  for (var i=0, max=centerAligned.length; i < max; i++) {
    centerAligned[i].style = "text-align: center;"
  }

  html = document.getElementById('text').innerHTML;
  html = '\
  <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>\
    <title>Document Title</title>\
    <xml>\
      <w:worddocument xmlns:w="#unknown">\
        <w:view>Print</w:view>\
        <w:zoom>90</w:zoom>\
        <w:donotoptimizeforbrowser />\
      </w:worddocument>\
    </xml>\
  </head>\
  <body lang=RU-ru style="tab-interval:.5in">\
    <div class="Section1">' + html + '</div>\
  </body>\
  </html>'

  blob = new Blob(['\ufeff', css + html], {
    type: 'application/msword'
  });

  url = URL.createObjectURL(blob);
  link = document.createElement('A');
  link.href = url;

  filename = 'filename';

  // Set default file name.
  // Word will append file extension - do not add an extension here.
  link.download = filename;   

  document.body.appendChild(link);

  if (navigator.msSaveOrOpenBlob) {
    navigator.msSaveOrOpenBlob( blob, filename + '.doc'); // IE10-11
  } else {
    link.click(); // other browsers
  }

  document.body.removeChild(link);
};

【讨论】:

  • 像魅力一样工作。对于今天的日期 05-12-2019,简单说一下,navigator.msSaveOrOpenBlob() 已弃用,不建议用于生产环境(请参阅 Mozilla 的文档)。还有一个简单的问题:我们如何为docx 文件实现相同的结果?
【解决方案2】:

我是一名 iOs 开发人员,但我能够将 html 转换为 .docx。 请将以下代码替换并添加到您的 HTML 文件中,并将文件扩展名更改为 .dcox/,doc

"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"  \"http://www.w3.org/TR/html4/loose.dtd\"><html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\"                xmlns=\"http://www.w3.org/TR/REC-html40\"><body>"

希望这对你有用。

【讨论】:

  • 你能让网页显示一个.docx文件吗?这是你想要做的。
  • 这似乎不起作用;我可以打开 doc 文件,但不能打开 docx 文件 sicne 文件,因为后者损坏了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-04
  • 1970-01-01
  • 2015-03-26
  • 1970-01-01
  • 2011-02-18
  • 1970-01-01
  • 2015-09-13
相关资源
最近更新 更多