1. Two ideas or principles for generating word with PHP
1. Use the com component under windows
2. Use PHP to write the content into the doc file
The specific implementation method is as follows.
2. Use com components under windows
Principle: com is an extension class of PHP. Servers with office installed will automatically call com of word.application, which can automatically generate documents. PHP official document manual: http://www.php.net/manual/en /class.com.php
Use official example:
3. Use PHP to write the content into the doc file
This method can be divided into two methods
1. Generate mht format (very similar to HTML) and write it into word
2. Write pure HTML format into word
1), generate mht format (very similar to HTML) and write it into word
The main function of this function is actually to analyze all the image addresses in the HTML code and download them in sequence. After obtaining the content of the image, call the MhtFileMaker class to add the image to the mht file. The specific adding details are encapsulated in the MhtFileMaker class.
Usage method 1: Remote call
Among them, the $content variable should be the HTML source code, and the following link should be the URL address that can fill in the relative path of the image in the HTML code
Usage method 2: Locally generated call
Note that before using this function, you need to include the class MhtFileMaker. This class can help us generate Mht documents.
点评:这种方法的缺点是不支持批量生成下载,因为一个页面只能有一个header,(无论远程使用还是本地生成声明header页面只能输出一个header),即使你循环生成,结果还是只有一个word生成(当然你可以修改上面的方式来实现)
2.纯HTML格式写入word
原理:
利用ob_start把html页面先存储起来(解决一下页面多个header问题,可以批量生成),然后在写入doc文档内容利用
代码:
PHP10086 | http://www.jb51.net |
PHP10086 | http://www.jb51.net |
PHP10086 最靠谱的PHP技术分享网站 |
第一代码比较简洁,很容易理解
第二是支持批量生成word(这个很重要)
第三是支持完整的html代码