PHP three ways to save a Web page as a Word file _php tutorial

Source: Internet
Author: User
One, PHP generation word of two ideas or principles

1. Take advantage of COM components under Windows
2. Use PHP to write content to the doc file
The implementation method is as follows.

Second, use the COM components under Windows

Principle: com as an extension of PHP class, installed Office server will automatically call Word.Application com, can automatically generate documents, PHP Official document manual: Http://www.php.net/manual/en/class.com.php

Use official examples:

Copy the Code code as follows: //Starting Word
$word = new COM ("Word.Application") or Die ("Unable to instantiate word");
echo "Loaded Word, Version {$word->version}\n";

Bring it to front
$word->visible = 1;

Open an empty document
$word->documents->add ();

Do some weird stuff
$word->selection->typetext ("This is a test ...");
$word->documents[1]->saveas ("Useless test.doc");

Closing word
$word->quit ();

Free the object
$word = null;
?>
Personal suggestion: The method after the COM instance needs to find the official document to know what meaning, the editor does not have the code hint, very inconvenient, moreover this efficiency is not very high, not recommended to use

Iii. using PHP to write content to a doc file
This method can be divided into two methods

1. Generate MHT format (very similar to HTML) to write to Word
2. Write to Word in plain HTML format


1), Generate MHT format (very similar to HTML) to write to Word

Copy CodeThe code is as follows:/**
* Get Word document content based on HTML code
* Create a document that is essentially MHT, which parses the contents of a file and downloads the picture resource from the page remotely
* This function depends on the class Mhtfilemaker
* This function parses the IMG tag and extracts the SRC attribute values. However, the attribute value of SRC must be surrounded by quotation marks, otherwise it cannot be extracted
*
* @param string $content HTML content
* @param string $absolutePath The absolute path of the Web page. If the image path in the HTML content is a relative path, you need to fill in this parameter to let the function automatically fill in the absolute path. This parameter finally needs to be/end
* @param bool $isEraseLink whether to remove links from HTML content
*/
function Getworddocument ($content, $absolutePath = "", $isEraseLink = True)
{
$MHT = new Mhtfilemaker ();
if ($isEraseLink)
$content = Preg_replace ('/(\s*.*?\s*) <\/a>/i ', ' $ ', $content); Remove link

$images = Array ();
$files = Array ();
$matches = Array ();
This algorithm requires that attribute values after src must be enclosed in quotation marks.
if (Preg_match_all ('//i ', $content, $matches))
{
$arrPath = $matches [1];
for ($i =0; $i {
$path = $arrPath [$i];
$imgPath = Trim ($path);
if ($imgPath! = "")
{
$files [] = $imgPath;
if (substr ($imgPath, 0,7) = = ' http//')
{
//absolute link, without prefix
}
Else
{
$imgPath = $absolut Epath. $imgPath;
}
$images [] = $imgPath;
}
}
}
$mht->addcontents ("tmp.html", $mht->getmimetype ("tmp.html"), $content);

for ($i =0; $i {
$image = $images [$i];
if (@fopen ($image, ' R '))
{
$imgcontent = @file_get_contents ($image);
if ($content)
$mht->addcontents ($files [$i], $mht->getmimetype ($image), $imgcontent);
}
Else
{
echo "file:". $image. "Not exist!
";
}
}

return $mht->getfile ();
}

The main function of this function is to parse all the image addresses in the HTML code and download them sequentially. After getting the contents of the picture, call the Mhtfilemaker class and add the picture to the MHT file. Specific add-on details, encapsulated in the Mhtfilemaker class.

Use Method 1: Remote Call
Copy the Code code 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, the following link should be able to fill the HTML code in the image relative to the path of the URL address


Where the $content variable should be the HTML source code, the following link should be able to fill the HTML code in the image relative to the path of the URL address

Use Method 2: Generate calls locally
Copy the Code code 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 can help us generate the MHT document.

Copy CodeThe code is as follows: /***********************************************************************
CLASS:MHT File Maker
version:1.2 Beta
date:02/11/2007
Author:wudi
Description:the class can make. mht file.
***********************************************************************/

Class mhtfilemaker{
var $config = array ();
var $headers = array ();
var $headers _exists = Array ();
var $files = array ();
var $boundary;
var $dir _base;
var $page _first;

function Mhtfile ($config = Array ()) {

}

function SetHeader ($header) {
$this->headers[] = $header;
$key = Strtolower (substr ($header, 0, Strpos ($header, ': ')));
$this->headers_exists[$key] = TRUE;
}

function Setfrom ($from) {
$this->setheader ("From: $from");
}

function Setsubject ($subject) {
$this->setheader ("Subject: $subject");
}

function SetDate ($date = NULL, $istimestamp = FALSE) {
if ($date = = NULL) {
$date = time ();
}
if ($istimestamp = = TRUE) {
$date = Date (' d, D M Y h:i:s O ', $date);
}
$this->setheader ("Date: $date");
}

function setboundary ($boundary = NULL) {
if ($boundary = = NULL) {
$this->boundary = '--'. Strtoupper (MD5 (Mt_rand ())). ' _multipart_mixed ';
} else {
$this->boundary = $boundary;
}
}

function Setbasedir ($dir) {
$this->dir_base = str_replace ("\ \", "/", Realpath ($dir));
}

function Setfirstpage ($filename) {
$this->page_first = str_replace ("\ \", "/", Realpath ("{$this->dir_base}/$filename"));
}

function Autoaddfiles () {
if (!isset ($this->page_first)) {
Exit (' not set the first page. ');
}
$filepath = Str_replace ($this->dir_base, ", $this->page_first);
$filepath = ' Http://mhtfile '. $filepath;
$this->addfile ($this->page_first, $filepath, NULL);
$this->adddir ($this->dir_base);
}

function Adddir ($dir) {
$handle _dir = Opendir ($dir);
while ($filename = Readdir ($handle _dir)) {
if ($filename! = ') && ($filename! = ' ... ') && ("$dir/$filename"! = $this->page_first)) {
if (Is_dir ("$dir/$filename")) {
$this->adddir ("$dir/$filename");
} elseif (Is_file ("$dir/$filename")) {
$filepath = Str_replace ($this->dir_base, "," $dir/$filename ");
$filepath = ' Http://mhtfile '. $filepath;
$this->addfile ("$dir/$filename", $filepath, NULL);
}
}
}
Closedir ($handle _dir);
}

function AddFile ($filename, $filepath = null, $encoding = null) {
if ($filepath = = NULL) {
$filepath = $filename;
}
$mimetype = $this->getmimetype ($filename);
$filecont = file_get_contents ($filename);
$this->addcontents ($filepath, $mimetype, $filecont, $encoding);
}

function addcontents ($filepath, $mimetype, $filecont, $encoding = NULL) {
if ($encoding = = NULL) {
$filecont = Chunk_split (Base64_encode ($filecont), 76);
$encoding = ' base64 ';
}
$this->files[] = array (' filepath ' = = $filepath,
' MimeType ' = $mimetype,
' Filecont ' = $filecont,
' Encoding ' = $encoding);
}

function Checkheaders () {
if (!array_key_exists (' Date ', $this->headers_exists)) {
$this->setdate (NULL, TRUE);
}
if ($this->boundary = = NULL) {
$this->setboundary ();
}
}

function Checkfiles () {
if (count ($this->files) = = 0) {
return FALSE;
} else {
return TRUE;
}
}

function GetFile () {
$this->checkheaders ();
if (! $this->checkfiles ()) {
Exit (' No file was added. ');
}
$contents = Implode ("\ r \ n", $this->headers);
$contents. = "\ r \ n";
$contents. = "mime-version:1.0\r\n";
$contents. = "content-type:multipart/related;\r\n";
$contents. = "\tboundary=\" {$this->boundary}\ "; \ r \ n";
$contents. = "\ttype=\" ". $this->files[0][' mimetype '). "\" \ r \ n ";
$contents. = "x-mimeole:produced by Mht File Maker v1.0 beta\r\n";
$contents. = "\ r \ n";
$contents. = "This was a multi-part message in MIME format.\r\n";
$contents. = "\ r \ n";
foreach ($this->files as $file) {
$contents. = "--{$this->boundary}\r\n";
$contents. = "Content-type: $file [mimetype]\r\n";
$contents. = "Content-transfer-encoding: $file [encoding]\r\n";
$contents. = "Content-location: $file [filepath]\r\n";
$contents. = "\ r \ n";
$contents. = $file [' Filecont '];
$contents. = "\ r \ n";
}
$contents. = "--{$this->boundary}--\r\n";
return $contents;
}

function MakeFile ($filename) {
$contents = $this->getfile ();
$fp = fopen ($filename, ' w ');
Fwrite ($fp, $contents);
Fclose ($FP);
}

function GetMimeType ($filename) {
$pathinfo = PathInfo ($filename);
Switch ($pathinfo [' extension ']) {
Case ' htm ': $mimetype = ' text/html '; Break
Case ' html ': $mimetype = ' text/html '; Break
Case ' txt ': $mimetype = ' text/plain '; Break
Case ' cgi ': $mimetype = ' text/plain '; Break
Case ' php ': $mimetype = ' text/plain '; Break
Case ' CSS ': $mimetype = ' text/css '; Break
Case ' jpg ': $mimetype = ' image/jpeg '; Break
Case ' jpeg ': $mimetype = ' image/jpeg '; Break
Case ' JPE ': $mimetype = ' image/jpeg '; Break
Case ' gif ': $mimetype = ' image/gif '; Break
Case ' png ': $mimetype = ' image/png '; Break
Default: $mimetype = ' application/octet-stream '; Break
}
return $mimetype;
}
}
?>

Reviews: The disadvantage of this approach is that bulk generation of downloads is not supported because a page can have only one header, (either remotely or locally generated claims header page can only output one header), even if you loop generation, The result is only one word generation (you can, of course, modify the method above)

2. Write to Word in plain HTML format

Principle:

Use Ob_start to store HTML pages first (solve multiple header problems on the page, you can generate batches), and then write the doc document content using

Code:
Copy the Code code as follows: class Word
{
function Start ()
{
Ob_start ();
Echo ' xmlns:w= "Urn:schemas-microsoft-com:office:word"
xmlns= "HTTP://WWW.W3.ORG/TR/REC-HTML40" > ";
}
function Save ($path)
{

echo "";
$data = Ob_get_contents ();
Ob_end_clean ();

$this->wirtefile ($path, $data);
}

function Wirtefile ($FN, $data)
{
$FP =fopen ($FN, "WB");
Fwrite ($fp, $data);
Fclose ($FP);
}
}
Copy the Code code as follows: $html = '













PHP10086 Http://www.jb51.net
PHP10086 Http://www.jb51.net

PHP10086

The most reliable PHP technology sharing site


';

Batch Build
for ($i =1; $i <=3; $i + +) {
$word = new Word ();
$word->start ();
$html = "AAA". $i;
$wordname = ' php tutorial site--jb51.net '. $i. ". Doc ";
Echo $html;
$word->save ($wordname);
Ob_flush ();//flush cache before each execution
Flush ();
}
Personal Comments:This method works best for three reasons:

The first code is relatively concise and easy to understand.
The second is to support batch generation of Word (this is important)
The third is to support the full HTML code

http://www.bkjia.com/PHPjc/748163.html www.bkjia.com true http://www.bkjia.com/PHPjc/748163.html techarticle first, the PHP generation of two ideas or principles of Word 1. Take advantage of the COM components under Windows 2. Use PHP to write content to a doc file by implementing the following method. Second, the use of Windows under the COM group ...

  • 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.