Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does php generate word documents from web pages and provide download function in the program?

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "php how to generate word documents in the program and provide download function", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "php how to generate word documents from web pages in the program and provide download function"!

In this article, we mainly solve two problems:

1: how to generate the contents of html into word documents in php

When 2:php generates the content in html into the word document, the problem is not centered, and it will be displayed according to the web view by default.

Related style incompatibility problems when 3:php generates the contents of html into word documents

Text:

The copy code is as follows:

Echo'

Print

'

Echo'

'

Echo "Digital Teaching system Electronic lesson preparation manuscript"

Subject Chinese School Experimental Middle School

"

Echo''

Ob_start (); / / Open the 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 browser

Note: the above code section provides the function of generating content in the php program file into the word document and downloading it.

In view of the second problem, the download to the local word document is displayed according to the web view by default after opening the document: as shown below:

Display 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 to set it (blue code): Print. The word document downloaded to the local file will be displayed as shown below:

The third problem is that there are some style incompatibility problems, such as the underlining of the relevant attributes under the top headline:

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

The solution is to change according to the subscript style recognized by word, that is, after changing to this style, there will be an underlining display after the download to the local word document is opened.

It is better to teach fish than to fish. I would like to share with you my solution to the incompatibility of this solution:

One: find a web version of the online editor, and then enter a few words in it, and then add an underline

Second: then click the View Source button at the top of the editor, and you can see that the property underlined just now is text-decoration: underline; instead of the style tag in html: border-bottom: 1px solid # 545454

At this point, I believe you have a deeper understanding of "php how to generate word documents in the program and provide download function". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report