重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
這篇文章給大家分享的是有關怎么將PHP網頁導出為Word文件的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
業務包括:企業網站建設、手機網站制作設計、商城系統網站開發、網站營銷推廣等服務,并且涵蓋域名注冊、虛擬主機、云服務器、等互聯網基礎服務;創新互聯聯建站以互聯網的創新理念,成熟完善的建站體系,開拓進取的精神,專業的解決方案和顧問咨詢服務,幫助客戶在互聯網時代提升形象、把握商機、實現價值,提高企業的核心競爭力。
原理
一般,有2種方法可以導出doc文檔,一種是使用com,并且作為php的一個擴展庫安裝到 實現 通過以上的原理介紹,相信大家應該對實現的過程有個初步的了解了,下面我給出一個導出函數,這個函數可以將HTML代碼導出成一個mht文檔,參數有3個,其中后2個為可選參數 content:要轉換的HTML代碼 absolutePath: 如果HTML代碼中的圖片地址都是相對路徑,那么這個參數就是HTML代碼中缺少的絕對路徑。 isEraseLink:是否去掉HTML代碼中的超鏈接 返回值為mht的文件內容,您可以通過file_put_content將它保存成后綴名為doc的文件 這個函數的主要功能其實就是分析HTML代碼中的所有圖片地址,并且依次下載下來。獲取到了圖片的內容以后,調用MhtFileMaker類,將圖片添加到mht文件中。具體的添加細節,封裝在MhtFileMaker類中了。 PHP代碼 使用方法: PHP代碼 其中,$content變量應該是HTML源代碼,后面的鏈接應該是能填補HTML代碼中圖片相對路徑的URL地址 注意,在使用這個函數之前,您需要先包含類MhtFileMaker,這個類可以幫助我們生成Mht文檔。 PHP代碼 上面我們討論了通過mht文件,來實現PHP導出doc格式的。這種方法可以解決一個難題,就是使導出的doc文件中包含圖片,當然,如果您要包含更多的內容,比如CSS樣式表,只需要用正則表達式分析HTML代碼中的link標簽,提取css樣式文件的地址,然后讀取并編碼成base64,最后加入到mht文件中就可以了。 感謝各位的閱讀!關于“怎么將PHP網頁導出為Word文件”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!/**
* 根據HTML代碼獲取word文檔內容
* 該函數依賴于類MhtFileMaker
*
* @param string $content HTML內容
* @param bool $isEraseLink 是否去掉HTML內容中的鏈接
*/
function getWordDocument( $content , $absolutePath = "" , $isEraseLink = true )
{
$mht = new MhtFileMaker();
if ( $isEraseLink )
$content = preg_replace( '/
$images = array ();
$files = array ();
$matches = array ();
//這個算法要求src后的屬性值必須使用引號括起來
if ( preg_match_all( '/
{
$arrPath = $matches [1];
for ( $i =0; $i < count ( $arrPath ); $i ++)
{
$path = $arrPath [ $i ];
$imgPath = trim( $path );
if ( $imgPath != " " )
{
$files[] = $imgPath;
if( substr($imgPath,0,7) == 'http://')
{
//絕對鏈接,不加前綴
}
else
{
$imgPath = $absolutePath.$imgPath;
}
$images[] = $imgPath;
}
}
}
$mht->AddContents(" tmp.html ",$mht->GetMimeType(" tmp.html "),$content);
for ( $i=0;$i
$fileContent = getWordDocument( $content , "http://www.yoursite.com/Music/etc/" );
$fp = fopen ( "test.doc" , 'w' );
fwrite( $fp , $fileContent );
fclose( $fp );
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 is 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 ;
}
}
?>
分享標題:怎么將PHP網頁導出為Word文件
網頁地址:http://www.xueling.net.cn/article/iejgci.html