php 如何在php中将html转换为doc
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4975266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert html to doc in php
提问by Mohit Jain
I need to convert a html file to doc. I am using html2pdf for pdf conversion.
我需要将 html 文件转换为 doc。我正在使用 html2pdf 进行 pdf 转换。
Is there is any same kind of library for html2doc?
html2doc 是否有相同类型的库?
(PS must be free/open source)
(PS必须是免费/开源的)
EDIT
编辑
After Mark Eirich comment..
在 Mark Eirich 发表评论后..
Here are two screenshots. Word document is not proper aligned. Check y-scroll in word document. WORD document, check y scroll..
这是两个屏幕截图。Word 文档未正确对齐。检查 Word 文档中的 y 滚动。 WORD文档,检查y滚动..
Body tag is:--
正文标签是:--
<body style="margin-left:350px; margin-right:350px;">
I tried to adjust it but no effect..
我试图调整它但没有效果..
EDIT 2
编辑 2
after Mark Eirich second comment i came to know word is taking things in pixel not in %age.. I am having last issue of back ground.. Any help.. please check the two screen shots. The difference is outer box. and thats y html generated doc is looking odd.
在 Mark Eirich 第二条评论之后,我开始知道 Word 正在以像素而不是 %age 中的内容取东西。区别在于外箱。这就是 y 生成的 html 文档看起来很奇怪。
回答by RobertPitt
The answer IMO Would be no, For the following reasons:
IMO 的答案是否定的,原因如下:
Microsoft Office Documents are extremely complex in the way they are designed, there not just a formatted file with references to objects such as images, there is a type od file system within itself to manage the binary data of these objects.
Microsoft Office Documents 的设计方式极其复杂,不仅有对图像等对象的引用的格式化文件,而且其内部还有一个类型 od 文件系统来管理这些对象的二进制数据。
Let me bring in a quote from our very own Joel:
让我引用我们自己的乔尔的话:
If you started reading these documents with the hope of spending a weekend writing some spiffy code that imports Word documents into your blog system, or creates Excel-formatted spreadsheets with your personal finance data, the complexity and length of the spec probably cured you of that desire pretty darn quickly. A normal programmer would conclude that Office's binary file formats:
如果您开始阅读这些文档是希望花一个周末时间编写一些漂亮的代码,将 Word 文档导入到您的博客系统中,或者使用您的个人财务数据创建 Excel 格式的电子表格,那么规范的复杂性和长度可能会让您感到困惑渴望很快。一个普通的程序员会得出这样的结论:Office 的二进制文件格式:
- are deliberately obfuscated
- are the product of a demented Borg mind
- were created by insanely bad programmers
- and are impossible to read or create correctly.
- 被故意混淆
- 是疯狂的博格思想的产物
- 是由非常糟糕的程序员创造的
- 并且无法正确阅读或创建。
You'd be wrong on all four counts....
你在所有四个方面都错了......
Read further down for a possible solution:
进一步阅读可能的解决方案:
If you really want to generate fancy formatted Word documents, your best bet is to create an RTF document. Everything that Word can do can be expressed in RTF, but it's a text format, not binary, so you can change things in the RTF document and it'll still work. You can create a nicely formatted document with placeholders in Word, save as RTF, and then using simple text substitution, replace the placeholders on the fly. Now you have an RTF document that every version of Word will open happily.
如果您真的想生成花哨格式的 Word 文档,最好的办法是创建一个 RTF 文档。Word 可以执行的所有操作都可以用 RTF 表示,但它是一种文本格式,而不是二进制格式,因此您可以更改 RTF 文档中的内容,它仍然可以工作。您可以在 Word 中使用占位符创建格式良好的文档,另存为 RTF,然后使用简单的文本替换,即时替换占位符。现在您有一个 RTF 文档,每个版本的 Word 都会愉快地打开它。
@source: http://www.joelonsoftware.com/items/2008/02/19.html
@source:http: //www.joelonsoftware.com/items/2008/02/19.html
Some links that may interest you along your journey:
您在旅途中可能会感兴趣的一些链接:
- Resources:
- Solutions
- 资源:
- 解决方案
Although, Try opening a word file with winrar ;), Maybe creating an archive with certain headers and then changing the extenstion will suffice, Never Tried
虽然,尝试使用 winrar 打开一个 word 文件;),也许创建一个带有某些标题的存档然后更改扩展名就足够了,从未尝试过
回答by Andrei Draganescu
in order to convert to Microsoft Word you need an COM enabled server (running Windows and Office on it). If you have such a server
为了转换为 Microsoft Word,您需要一个支持 COM 的服务器(在其上运行 Windows 和 Office)。如果你有这样的服务器
$word = new COM("word.application") or die ("couldnt create an instance of word");
should work!. Read http://php.net/manual/en/book.com.phpfor details.
应该管用!。阅读http://php.net/manual/en/book.com.php了解详情。
Otherwise your best shot at html2doc is html2rtf which is achieved with a library such as http://paggard.com/projects/rtf.generator/or http://sourceforge.net/projects/phprtf/.
否则,您在 html2doc 上的最佳选择是 html2rtf,它是通过诸如http://paggard.com/projects/rtf.generator/或http://sourceforge.net/projects/phprtf/ 之类的库实现的。
Then after you create the RTF you serve it to the browser with a doc header
然后在创建 RTF 后,您将其提供给浏览器并带有 doc 标题
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=document_name.doc");
If the user has word then it will be open to handle the file.
如果用户有字,那么它将被打开以处理该文件。
Also saving an rtf as doc is ok and word will open in layout view without any complaints. You can also serve HTML with the above header but the problem is that Word will open in web view and that is bad :)
还可以将 rtf 保存为 doc,并且 word 将在布局视图中打开而没有任何抱怨。您也可以使用上述标题提供 HTML,但问题是 Word 将在 Web 视图中打开,这很糟糕:)