简体   繁体   English

如何在php中从.doc模板创建一个单词.doc文件

[英]How to Create a word .doc file from a .doc template in php

I need to create a word document from a word template, its something like, we need to replace the few strings (say variables) in the template with the values. 我需要从一个单词模板创建一个单词文档,就像我们需要用值替换模板中的几个字符串(比如变量)。

Please tell how we can do this in PHP ( or cakePHP). 请告诉我们如何在PHP(或cakePHP)中执行此操作。

The biggest problem you will run into is that the .doc format is binary in nature (see this question and this post by Joel ). 您将遇到的最大问题是.doc格式本质上是二进制的(请参阅此问题Joel的这篇文章 )。

Is there any reason you cannot using either an HTML formatted document (which word has been able to open for over 10 years) or a Rich Text File ? 您是否有任何理由不能使用HTML格式的文档(哪个词能够打开超过10年)或富文本文件 Both of these are text based, and will cause you significantly less pain. 这些都是基于文本的,并且会使您的痛苦明显减少。 They are, as a result, very easy to use a template with. 因此,它们非常容易使用模板。

Alternatively, if you can be sure that the user will have a newer version of Office (2007+) then you could, though it is somewhat tricky, use a .docx document, which is actually a Zip Archive and XML data. 或者,如果您可以确定用户将拥有更新版本的Office(2007+),那么您可以使用.docx文档,尽管它有点棘手,它实际上是一个Zip存档和XML数据。 You can implement this fairly simply by creating a .docx word file that has the template you need, renaming it to .zip , extracting the files, teaching your application to do the text replacement on this in-place file structure, re-zip it using PHP's ZipArchive class , and then sending that file (named .docx , of course). 你可以通过创建一个包含所需模板的.docx word文件,将其重命名为.zip ,解压缩文件,教你的应用程序在这个就地文件结构上进行文本替换,重新压缩它来实现这一点。使用PHP的ZipArchive类 ,然后发送该文件(当然,名为.docx )。

i think this will help you 我想这会对你有所帮助

docxtemplater-master docxtemplater主

Generate docx from templates, https://github.com/edi9999/docxtemplater 从模板生成docx, https://github.com/edi9999/docxtemplater

phpdocx phpdocx

PHP library for the dynamical generation of Word documents in docx format. 用于以docx格式动态生成Word文档的PHP库。

http://www.phpdocx.com/ http://www.phpdocx.com/

Check http://www.phpbuilder.com/annotate/message.php3?id=1024286 and also the article it comments. 请查看http://www.phpbuilder.com/annotate/message.php3?id=1024286以及它评论的文章。

I made a similar trick when I needed to generate a simple excel table. 当我需要生成一个简单的excel表时,我做了类似的技巧。 When sending the right header, the client's excel didn't complain at all. 发送正确的标题时,客户端的excel根本没有抱怨。 Or if you can use another format directly, use it. 或者,如果您可以直接使用其他格式,请使用它。 (.docx or open document are compressed xml, so you can manipulate it more easily) (.docx或打开的文档是压缩的xml,因此您可以更轻松地操作它)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM