Php generates Word documents and provides the downloaded code in the program.

Source: Internet
Author: User

This article mainly solves two problems:

1: how to generate html content into WORD Documents in php
2: when php generates the html content into a Word document, the text is displayed according to the web View by default.
3: related style incompatibility issues when php generates html content into a Word document

Body:Copy codeThe Code is as follows: echo '<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Xml> <w: WordDocument> <w: View> Print </w: View> </xml>
<Script src = "schemdes/js/ztree/js/jquery-1.4.4.min.js" type = "text/javascript"> </script>
</Head> ';
Echo '<body> <table class = "table_dayin">
<Caption class = "table_caption"> ';
Echo "digital teaching system electronic course preparation draft <br>
<Span> subject <em style = "border-bottom: 1px solid #545454;"> language </em> school <em style = "border-bottom: 1px solid #545454; "> lab Middle School </em> </span>
</Caption> ";
Echo '</table> </body> 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"], 'msi ')){
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 provides the ability to generate content in the php program file into the word document and download it.
For the 2nd problems, the downloaded local Word documents are displayed according to the web View by default after being opened: for example:
Display in the default web View:

If the display is based on the normal page view, you need to add an xml line in the header for setting (in the Blue Code Section): <xml> <w: WordDocument> <w: view> Print </w: View> </xml>. After the Word file is added and downloaded to the local device, it is displayed as follows:

The third problem is style incompatibility issues. For example, the underlined display of relevant attributes under the top heading is as follows:

We added border-bottom: 1px solid #545454 in the html style (in the blue code section), that is, <em style = "border-bottom: 1px solid #545454; ">, but the underline is not displayed because it is not recognized in word. For example:

The solution is to change the subscript style recognized by the word, that is, <em style = "text-decoration: underline;">. After changing to this style, that is, the downloaded local Word document is displayed with an underline.

It is better to teach fish to fish. I will share with you my solution to this incompatible style:

I. Find a web-based online editor, enter a few texts in it, and add underscores (_).

2. Click the "view source code" button in the editor to view the underlined attribute text-decoration: underline, rather than the style label border-bottom in html: 1px solid #545454;

Now, we are here to address the above issues. If you have any questions, please come up and discuss and solve them together.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.