您好,登錄后才能下訂單哦!
php實現將HTML頁面轉換成word并且保存的方法?這個問題可能是我們日常學習或工作經常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家帶來的參考內容,讓我們一起來看看吧!
php將html轉成word的方法:首先安裝zip.dll壓縮擴展;然后將規定好了的xml壓縮成一個zip包;最后把后綴名改成doc或者docx即可。
php實現將HTML頁面轉換成word并且保存的方法
這里用使用到一個PHP的工具叫:PHPWord。
生成Word的原理是,將堆規定好了的xml壓縮成一個zip包,并且把后綴名改成doc或者docx即可。
所以使用PHPWord,需要你的PHP環境安裝zip.dll壓縮擴展,我寫了一個demo.
功能說明:
20150507 — HTML中的<p>標簽和<ol>列表標簽的獲取
20150508 — 新增獲取文章中的圖片功能
20150509 — 新增行間距,并且過濾一下錯誤圖片
20150514 — 新增表格處理,并且將代碼改成面向對象
20150519 — 新增GD庫處理網絡圖片
require_once 'PHPWord.php'; require_once 'SimpleHtmlDom.class.php'; class Word{ private $url; private $LinetextArr = array(); public $CurrentDir; public $error = array(); //錯誤數組 public $filename = null; public $Allowtag = "p,ol,ul,table"; /**數據統計**/ public $DownImg = 0; public $expendTime = 0; public $HttpRequestTime = 0; public $ContentLen = 0; public $HttpRequestArr = array(); public $expendmemory = 0; public function __construct($url) { $startTime = $this->_Time(); $startMemory = $this->_memory(); $this->url = $url; $UrlArr = parse_url($this->url); $this->host = $UrlArr["scheme"]."://".$UrlArr['host']; $this->CurrentDir = getcwd(); $this->LinetextArr["table"] = array(); $html = new simple_html_dom($this->url); $this->HttpRequestArr[] = $this->url; $this->HttpRequestTime++; foreach($html->find($this->Allowtag) as $key=>$value) { if($value->tag == "table") { $this->ParseTable($value,0,$this->LinetextArr["table"]); } else { $this->AnalysisHtmlDom($value); } $this->error[] = error_get_last(); } $endTime = $this->_Time(); $endMemory = $this->_memory(); $this->expendTime = round(($endTime-$startTime),2); //微秒 $this->expendmemory = round(($endMemory-$startMemory)/1000,2); //bytes $this->CreateWordDom(); } private function _Time() { return array_sum(explode(" ", microtime())); } private function _memory() { return memory_get_usage(); } /** * 解析HTML中的Table,這里考慮到多層table嵌套的情況 * @param $value HTMLDOM * @param $i 遍歷層級 * **/ private function ParseTable($value,$i,$Arr) { if($value->firstChild() && in_array($value->firstChild()->tag,array("table","tbody","thead","tfoot","tr"))) { foreach($value->children as $k=>$v) { $this->ParseTable($v,$i++,$Arr); } } else { foreach($value->children as $k=>$v) { if($v->firstChild() && $v->firstChild()->tag != "table") { $Arr[$i][] = array("tag"=>$v->tag,"text"=>trim($v->plaintext)); } if(!$v->firstChild()) { $Arr[$i][] = array("tag"=>$v->tag,"text"=>trim($v->plaintext)); } } } } /** * 解析HTML里面的表情 * @param $value HTMLDOM * **/ private function AnalysisHtmlDom($value) { $tmp = array(); if($value->has_child()) { foreach($value->children as $k=>$v) { $this->AnalysisHtmlDom($v); } } else { if($value->tag == "a") { $tmp = array("tag"=>$value->tag,"href"=>$value->href,"text"=>$value->innertext); } else if($value->tag == "img") { $src = $this->unescape($value->src); $UrlArr = parse_url($src); if(!isset($UrlArr['host'])) { $src = $this->host.$value->src; $UrlArr = parse_url($src); } $src = $this->getImageFromNet($src,$UrlArr); //表示有網絡圖片,需要下載 if($src) { $imgsArr = $this->GD($src); $tmp = array("tag"=>$value->tag,"src"=>$src,"text"=>$value->alt,"width"=>$imgsArr['width'],"height"=>$imgsArr['height']); } } else { $tmp = array("tag"=>$value->tag,"text"=>strip_tags($value->innertext)); } $this->LinetextArr[] = $tmp; } } /** * 根據GD庫來獲取圖片的如果太多,進行比例壓縮 * **/ private function GD($src) { list($width, $height, $type, $attr) = getimagesize($src); if($width > 800 || $height > 800 ) { $width = $width/2; $height = $height/2; } return array("width"=>$width,"height"=>$height); } /** * 將Uincode編碼轉移回原來的字符 * **/ public function unescape($str) { $str = rawurldecode($str); preg_match_all("/(?:%u.{4})|&#x.{4};|&#\d+;|.+/U",$str,$r); $ar = $r[0]; foreach($ar as $k=>$v) { if(substr($v,0,2) == "%u"){ $ar[$k] = iconv("UCS-2BE","UTF-8",pack("H4",substr($v,-4))); } elseif(substr($v,0,3) == "&#x"){ $ar[$k] = iconv("UCS-2BE","UTF-8",pack("H4",substr($v,3,-1))); } elseif(substr($v,0,2) == "&#"){ $ar[$k] = iconv("UCS-2BE","UTF-8",pack("n",substr($v,2,-1))); } } return join("",$ar); } /** * 圖片下載 * @param $Src 目標資源 * @param $UrlArr 目標URL對應的數組 * **/ private function getImageFromNet($Src,$UrlArr) { $file = basename($UrlArr['path']); $ext = explode('.',$file); $this->ImgDir = $this->CurrentDir."/".$UrlArr['host']; $_supportedImageTypes = array('jpg', 'jpeg', 'gif', 'png', 'bmp', 'tif', 'tiff'); if(isset($ext['1']) && in_array($ext['1'],$_supportedImageTypes)) { $file = file_get_contents($Src); $this->HttpRequestArr[] = $Src; $this->HttpRequestTime++; $this->_mkdir(); //創建目錄,或者收集錯誤 $imgName = md5($UrlArr['path']).".".$ext['1']; file_put_contents($this->ImgDir."/".$imgName,$file); $this->DownImg++; return $UrlArr['host']."/".$imgName; } return false; } /** * 創建目錄 * **/ private function _mkdir() { if(!is_dir($this->ImgDir)) { if(!mkdir($this->ImgDir,"7777")) { $this->error[] = error_get_last(); } } } /** * 構造WordDom * **/ private function CreateWordDom() { $PHPWord = new PHPWord(); $PHPWord->setDefaultFontName('宋體'); $PHPWord->setDefaultFontSize("11"); $styleTable = array('borderSize'=>6, 'borderColor'=>'006699', 'cellMargin'=>120); // New portrait section $section = $PHPWord->createSection(); $section->addText($this->Details(),array(),array('spacing'=>120)); //數據進行處理 foreach($this->LinetextArr as $key=>$lineArr) { if(isset($lineArr['tag'])) { if($lineArr['tag'] == "li") { $section->addListItem($lineArr['text'],0,"","",array('spacing'=>120)); } else if($lineArr['tag'] == "img") { $section->addImage($lineArr['src'],array('width'=>$lineArr['width'], 'height'=>$lineArr['height'], 'align'=>'center')); } else if($lineArr['tag'] == "p") { $section->addText($lineArr['text'],array(),array('spacing'=>120)); } } else if($key == "table") { $PHPWord->addTableStyle('myOwnTableStyle', $styleTable); $table = $section->addTable("myOwnTableStyle"); foreach($lineArr as $key=>$tr) { $table->addRow(); foreach($tr as $ky=>$td) { $table->addCell(2000)->addText($td['text']); } } } } $this->downFile($PHPWord); } public function Details() { $msg = "一共請求:{$this->HttpRequestTime}次,共下載的圖片有{$this->DownImg}張,并且下載完成大約使用時間:{$this->expendTime}秒,整個程序執行大約消耗內存是:{$this->expendmemory}KB,"; return $msg; } public function downFile($PHPWord) { if(empty($this->filename)) { $UrlArr = parse_url($this->url); $this->filename = $UrlArr['host'].".docx"; } // Save File $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $objWriter->save($this->filename); header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-Description: File Transfer"); //Use the switch-generated Content-Type header('Content-type: application/msword');//輸出的類型 //Force the download $header="Content-Disposition: attachment; filename=".$this->filename.";"; header($header); @readfile($this->filename); } }
上面的代碼重點感覺不是word生成,而是Simplehtmldom的使用,這是一個開源的HTML解析器,之前有提到,這幾天在看他的代碼,
引出了兩個學習方向
① 正在表達式
② 這個擴展的函數整理
看源代碼的收獲:
PHP的異常是可以捕獲的,而且PHP的錯誤也是可以捕獲的。
error_get_last() //用這個函數可以捕獲頁面中的PHP錯誤,不謝。
感謝各位的閱讀!看完上述內容,你們對php實現將HTML頁面轉換成word并且保存的方法大概了解了嗎?希望文章內容對大家有所幫助。如果想了解更多相關文章內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。