Home > Backend Development > PHP Tutorial > 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中文网
Release: 2023-02-28 21:42:01
Original
4371 people have browsed it

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 &#39;<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
<xml><w:WordDocument><w:View>Print</w:View></xml> 
<script src="includes/js/ztree/js/jquery-1.4.4.min.js" type="text/javascript"></script> 
</head>&#39;; 
echo &#39;<body><table class="table_dayin"> 
<caption class="table_caption">&#39;; 
echo "数字化教学系统电子备课稿<br> 
<span>学科 <em style="border-bottom: 1px solid #545454;">语文</em>学校 <em style="border-bottom: 1px solid #545454;">实验中学</em></span> 
</caption>"; 
echo &#39;</table></body></html>&#39;; 
ob_start(); //打开缓冲区 
header("Cache-Control: public"); 
Header("Content-type: application/octet-stream"); 
Header("Accept-Ranges: bytes"); 
if (strpos($_SERVER["HTTP_USER_AGENT"],&#39;MSIE&#39;)) { 
header(&#39;Content-Disposition: attachment; filename=test.doc&#39;); 
}else if (strpos($_SERVER["HTTP_USER_AGENT"],&#39;Firefox&#39;)) { 
Header(&#39;Content-Disposition: attachment; filename=test.doc&#39;); 
} else { 
header(&#39;Content-Disposition: attachment; filename=test.doc&#39;); 
} 
header("Pragma:no-cache"); 
header("Expires:0"); 
ob_end_flush();//输出全部内容到浏览器
Copy after login

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): Print, 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: , 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: . 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

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template