Home  >  Article  >  Backend Development  >  Generate a web page into a word document in the php program and provide downloadable example code

Generate a web page into a word document in the php program and provide downloadable example code

PHP中文网
PHP中文网Original
2017-03-22 14:06:514233browse

Generate content in the php program file into the word document and provide the implementation code for the download function. Friends who need it can refer to it

In this article, we mainly solve two problems:
1: How to put html in php Generate the content in the html into the word document
2: When php generates the content in html into the word document, the problem is not displayed in the center, that is, it will be displayed according to the web view by default.
3: When PHP generates content in HTML into a word document, related style incompatibility issues
Text:

echo ' 
 
 
Print 
 

'; echo '"; echo '
'; echo "数字化教学系统电子备课稿
学科 语文学校 实验中学
'; ob_start(); //打开缓冲区 header("Cache-Control: public"); Header("Content-type: application/octet-stream"); Header("Accept-Ranges: bytes"); if (strpos($_SERVER["HTTP_USER_AGENT"],'MSIE')) { header('Content-Disposition: attachment; filename=test.doc'); }else if (strpos($_SERVER["HTTP_USER_AGENT"],'Firefox')) { Header('Content-Disposition: attachment; filename=test.doc'); } else { header('Content-Disposition: attachment; filename=test.doc'); } header("Pragma:no-cache"); header("Expires:0"); ob_end_flush();//输出全部内容到浏览器

Note: The above code part provides the function of generating content in the PHP program file into a word document and providing a download function.
In response to the second question, after the word document downloaded to the local is opened, it is displayed in the web view by default: as shown below:
Displayed in the default web view:

If it is displayed in the normal page view, You need to add a line of xml mark in the header for setting (blue code part): b2a0af5a8fd26276da50279a1c63a57a2facf99ecb5ae18e033c998b2258c8a5cbfa6c478d298339139d506edd72ca79Print8bcb4f45e4725f668e29c18d94c1940521118965b89073f60271ef4a3b5d3c58, add it and download it to The local word document displays as shown below after opening:

Regarding the third problem, there are some style incompatibility issues, such as the underline annotation of the relevant attributes under the big title at the top:

Our style in html The style border-bottom: 1px solid #545454; has been added (blue code part), that is: 3ce2b70afc7852ca0bd3ae803dc82461, but the underline is still not displayed because in Not recognized in word. As shown below:

The solution is to change it according to the subscript style recognized by word, that is: 3d2ffbb749be2c59df766cb2886c672a. After changing to this style, it will be downloaded to the local After the word document is opened, the underline mark is displayed.

It is better to teach someone to fish than to teach him to fish. I will share with you my solution to this style incompatibility:

1: Find a web version of the online editor, and then do whatever you want in it Enter a few words, and then add an underline mark

2: Then click the View Source Code button on the editor, you can see that the underlined attribute just added is text-decoration: underline; instead of the style mark in html :border-bottom: 1px solid #545454;

Okay, that’s it for the above related issues. If you have any questions, please ask them and we will discuss and solve them together.

Related articles:

php uses phpword to generate word documents

How to export and generate word using php

Three ways to implement PHP to generate word documents

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP debugging tool XdebugNext article:PHP debugging tool Xdebug