如何在php中将html转换为doc

发布于 2024-10-17 02:16:39 字数 1540 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

今天小雨转甜 2024-10-24 02:16:39

IMO 的答案是否定的,原因如下:

Microsoft Office 文档的设计方式极其复杂,不仅仅是一个引用图像等对象的格式化文件,其内部还有一个 od 类型的文件系统需要管理这些对象的二进制数据。

让我引用我们自己的乔尔的话:

如果您开始阅读这些文档,希望花一个周末编写一些漂亮的代码,将 Word 文档导入您的博客系统,或者使用您的个人财务数据创建 Excel 格式的电子表格,那么规范的复杂性和长度可能会得到解决你的这种愿望很快就会实现。普通程序员会得出这样的结论:Office 的二进制文件格式:

  • 是: 故意混淆的
  • 是疯狂 Borg 思想的产物
  • 由极其糟糕的程序员创建的
  • ,并且无法正确读取或创建。

你在所有四个方面都错了......

继续阅读以获取可能的解决方案:

如果您确实想生成格式精美的 Word 文档,最好的选择是创建 RTF 文档。 Word 可以做的所有事情都可以用 RTF 表示,但它是文本格式,而不是二进制,因此您可以更改 RTF 文档中的内容,它仍然有效。您可以在 Word 中创建带有占位符的格式良好的文档,另存为 RTF,然后使用简单的文本替换,即时替换占位符。现在您有了一个 RTF 文档,每个版本的 Word 都可以轻松打开该文档。

@source:http://www.joelonsoftware.com/items/2008/02 /19.html

您在旅途中可能会感兴趣的一些链接:

虽然,尝试使用 winrar 打开一个 word 文件;),也许创建一个具有某些标题的存档,然后更改扩展名就足够了,从未尝试过

The answer IMO Would be no, For the following reasons:

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.

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:

  • 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.

@source: http://www.joelonsoftware.com/items/2008/02/19.html

Some links that may interest you along your journey:

Although, Try opening a word file with winrar ;), Maybe creating an archive with certain headers and then changing the extenstion will suffice, Never Tried

时光清浅 2024-10-24 02:16:39

为了转换为 Microsoft Word,您需要一个支持 COM 的服务器(在其上运行 Windows 和 Office)。如果你有这样的服务器

$word = new COM("word.application") or die ("couldnt create an instance of word"); 

应该可以工作!阅读 http://php.net/manual/en/book.com.php 了解详情。

否则,您最好的 html2doc 是 html2rtf,它是通过 http://paggard.com/ 等库实现的items/rtf.generator/http://sourceforge.net/projects/phprtf/

然后,在创建 RTF 后,您可以使用文档标题将其提供给浏览器。

header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=document_name.doc");

如果用户有单词,则它将打开以处理该文件。

另外,将 rtf 保存为 doc 是可以的,并且 word 将在布局视图中打开,没有任何抱怨。
您还可以使用上述标头提供 HTML,但问题是 Word 将在 Web 视图中打开,这很糟糕:)

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

$word = new COM("word.application") or die ("couldnt create an instance of word"); 

should work!. Read http://php.net/manual/en/book.com.php for details.

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/.

Then after you create the RTF you serve it to the browser with a doc header

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文