In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to save the web page as a word file by PHP". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to save the web page as a word file by PHP".
1. Two ideas or principles of generating word by PHP
1. Use the com components under windows
two。 Use PHP to write the content into the doc file
The specific implementation methods are as follows.
Second, use the com components under windows
Principle: com as an extension of PHP, the server installed with office will automatically call word.application 's com, can automatically generate documents, PHP official documentation manual: http://www.php.net/manual/en/class.com.php
Use an official example:
The copy code is as follows:
Personal suggestion: the method after the com instance needs to find the official document to know what it means. The editor has no code hints, which is very inconvenient. In addition, this efficiency is not very high, and it is not recommended to use it.
Third, use PHP to write the content into the doc file
This method can be divided into two methods.
1. Generate mht format (very similar to HTML) to write to word
two。 Write to word in pure HTML format
1), generate mht format (similar to HTML) and write to word
The copy code is as follows:
/ * *
* obtain word document content according to HTML code
* create a document that is essentially mht. This function analyzes the contents of the file and downloads the image resources on the page remotely.
* this function depends on the class MhtFileMaker
* this function analyzes the img tag and extracts the attribute value of src. However, the attribute value of src must be surrounded by quotation marks, otherwise it cannot be extracted
*
* @ param string $content HTML content
The absolute path to the * @ param string $absolutePath web page. If the image path in the HTML content is a relative path, you need to fill in this parameter to make the function automatically fill in the absolute path. This parameter finally needs to end with /
* @ param bool $isEraseLink whether to remove links from HTML content
, /
Function getWordDocument ($content, $absolutePath = "", $isEraseLink = true)
{
$mht = new MhtFileMaker ()
If ($isEraseLink)
$content = preg_replace ('/ (\ sroom.links?\ s *) / i','$1', $content); / / remove the link
$images = array ()
$files = array ()
$matches = array ()
/ / this algorithm requires that the attribute values after src must be enclosed in quotation marks
If (preg_match_all ('/ GetMimeType ("tmp.html"), $content)
For ($iAddContents ($files [$I], $mht- > GetMimeType ($image), $imgcontent)
}
Else
{
Echo "file:". $image. "not exist!"
}
}
Return $mht- > GetFile ()
}
The main function of this function is to analyze all the image addresses in the HTML code and download them in turn. After getting the contents of the picture, call the MhtFileMaker class to add the picture to the mht file. The specific addition details are encapsulated in the MhtFileMaker class.
Use method 1: call remotely
The copy code is as follows:
$url= http://www.***.com;
$content = file_get_contents ($url)
$fileContent = getWordDocument ($content, "http://www.yoursite.com/Music/etc/");
$fp = fopen ("test.doc",'w')
Fwrite ($fp, $fileContent)
Fclose ($fp)
Where the $content variable should be the HTML source code, and the following link should be the URL address that can fill the relative path of the picture in the HTML code.
Where the $content variable should be the HTML source code, and the following link should be the URL address that can fill the relative path of the picture in the HTML code.
Use method 2: locally generate calls
The copy code is as follows:
Header ("Cache-Control: no-cache, must-revalidate")
Header ("Pragma: no-cache")
$wordStr = 'PHP tutorial website-jb51.net'
$fileContent = getWordDocument ($wordStr)
$fileName = iconv ("utf-8", "GBK", "PHP tutorial". '_'. $intro. '_'. Rand (100,999)
Header ("Content-Type: application/doc")
Header ("Content-Disposition: attachment; filename=". $fileName. ".doc")
Echo $fileContent
Note that before using this function, you need to include the class MhtFileMaker, which helps us generate Mht documents.
The copy code is as follows:
Comments: the disadvantage of this method is that batch generation and download is not supported, because there can be only one header for a page (whether it is used remotely or locally, it is declared that a header page can only output one header). Even if you generate it in a loop, the result is still only one word generation (of course, you can modify the above method to achieve it).
two。 Write to word in pure HTML format
Principle:
Use ob_start to store the html page first (solve multiple header problems of the page, which can be generated in batches), and then use the
Code:
The copy code is as follows:
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.