Home >Backend Development >PHP Tutorial >PHP generates a word document from a web page in the program and provides the downloaded code_PHP tutorial

PHP generates a word document from a web page in the program and provides the downloaded code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:15:46909browse

This article mainly solves two problems:

1: How to generate the content in html into a word document in php
2: php converts the content in html When generated into a word document, if the problem is not displayed in the center, it will be displayed in the web view by default.
3: When PHP generates content from HTML into a word document, related style incompatibility issues

Text:

Copy code The code is as follows:

echo '


Print

';
echo '< body>
";
echo '
';
echo "Digital Teaching System Electronic Lesson Preparation Manuscript

Subject ChineseSchoolExperimental Middle School
';
ob_start(); //Open buffer
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();//Output all content to the browser

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

If it is displayed according to the normal page view, you need to add a line of xml mark in the header for setting (blue code part): Print, after adding, the word document downloaded to the local area will display as shown below when opened:

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

We added the border-bottom: 1px solid #545454; style (blue code part) to the style in html, that is: , but the underline is still not displayed because it is not recognized in word. As shown below:

The solution is to change it according to the subscript style recognized by word, that is: . After changing to this style, that is, after opening the word document downloaded to the local It will be underlined.

It is better to teach a man 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, then enter a few words in it, and then add an underline

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326047.htmlTechArticleIn this article, we mainly solve two problems: 1: How to generate the content in html to 2 in word document: When php generates the content in html into the word document, the display problem is not centered...
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