How PHP exports word documents quickly-PHP Tutorial

Source: Internet
Author: User
Quickly learn how PHP exports Word documents. Generally, there are two ways to export doc files. one is to use com and install it on the server as an extension library of php, and then create a com and call its method. Installed

Generally, there are two ways to export doc files. one is to use com and install it on the server as an extension library of php, and then create a com and call its method. The server that has installed the office can call a name called word. application com can generate Word documents, but I do not recommend this method because the execution efficiency is relatively low (I tested it, during code execution, the server will actually open a word client ). The ideal com should have no interface and perform data conversion in the background. This will produce better results, but these extensions are generally charged.

The 2nd method is to use PHP to export the content of a Word document and directly write it into a file suffixed with doc. To use this method, you do not need to rely on third-party extensions, and the execution efficiency is high.

Word itself is very powerful, it can open html files, and can retain the format, even if the suffix is doc, it can recognize the normal open. This provides us with convenience. However, there is a problem: the image in an html file has only one address, and the real image is saved elsewhere. that is to say, if the HTML format is written to the doc, therefore, the doc cannot contain images. How can we create a doc file containing images? We can use mht format close to html.

The mht format is similar to html, but in the mht format, files that are externally linked, such as slice, Javascript, and CSS, are encoded and stored by Base64. Therefore, a single mht file can store all the resources in a web page. of course, the size of a single mht file is larger than that of html.

Can the mht format be recognized by word? I save a webpage as mht, modify the suffix to doc, and use word to open it. OK and word can also identify the mht file and display images.

Now that doc can recognize mht, we will consider how to put the image into mht. Because the image addresses in html code are all written in the src attribute of the img tag, you only need to extract the src attribute value in html code to obtain the image address. Of course, it is possible that you get the relative path, it does not matter, add the URL prefix, change to the absolute path. With the image address, we can use the file_get_content function to obtain the specific content of the image file, and then call the base64_encode function to encode the file content into base64 encoding, insert it to the appropriate location of the mht file.

Finally, we have two PHP methods to export Word documents to send files to the client. one is to first generate a doc file on the server side, then record the URL of the doc file. finally, you can use header ("location: xx.doc") to download the doc from the client. Another method is to directly send an html request, modify the header of the HTML protocol, set its content-type to application/doc, and set content-disposition to attachment, followed by the file name, after the html protocol is sent, you can directly send the file content to the client. you can also download the file to the client.


Bytes. Installed...

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.