php實現word轉html文檔的例子


php實現word轉html文檔的例子

word文檔不適合放到網頁上了,如果我們要放到網頁中去是需要一個個復制了,如果你還在復制就out了,下文小編來為各位整理一篇php實現word轉html文檔的例子,希望文章對各位有幫助。

要想完美解決,office轉pdf或者html,最好還是用windows office軟件,libreoffice不能完美轉換,wps沒有api。

先確認com模塊是不是開啟,phpinfo里面如果有com_dotnet模塊,說明已開啟,如果沒有,修改PHP.ini,
com.allow_dcom = true

前面的注釋去掉,重啟就OK了,php官方網站說,php5.4.5之前,com模塊是內置的,其實也不一定全是,官網下的php 5.3.39,com模塊就沒有內置。

如果不是內置模塊的話,php.ini加上,前提你的ext文件夾下,有該擴展

extension=php_com_dotnet.dll

然后重啟就OK了

 

[php]  view plain  copy
 
 print?
  1. function word2html($wordname,$htmlname)   
  2.  {   
  3.  $word = new COM("word.application") or die("Unable to instanciate Word");   
  4.  $word->Visible = 1;   
  5.  $word->Documents->Open($wordname);   
  6.  $word->Documents[1]->SaveAs($htmlname,8);   
  7.  $word->Quit();   
  8.  $word = null;   
  9.  unset($word);   
  10.  }   
  11.    
  12. word2html('D:/www/test/6.docx','D:/www/test/6.html');   

注意:

1,轉換出來的html,查看源碼,比較亂的
2,轉換過程中會調用winword.exe
3,如果頁面一直在加載,把文檔重命名,然后在重新轉。

補充一個例子

 

[php]  view plain  copy
 
 print?
  1. function lego_clean($text) {    
  2.    
  3.     $text = implode("\r",$text);    
  4.    
  5.     // normalize white space    
  6.     $text = eregi_replace("[[:space:]]+", " ", $text);    
  7.     $text = str_replace("> <",">\r\r<",$text);    
  8.     $text = str_replace("<br>","<br>\r",$text);    
  9.    
  10.     // remove everything before <body>    
  11.     $text = strstr($text,"<body");    
  12.    
  13.     // keep tags, strip attributes    
  14.     $text = ereg_replace("<p [^>]*BodyTextIndent[^>]*>([^\n|\n\015|\015\n]*)</p>","<p>\\1</p>",$text);    
  15.     $text = eregi_replace("<p [^>]*margin-left[^>]*>([^\n|\n\015|\015\n]*)</p>","<blockquote>\\1</blockquote>",$text);    
  16.     $text = str_replace(" ","",$text);    
  17.    
  18.     //clean up whatever is left inside <p> and <li>    
  19.     $text = eregi_replace("<p [^>]*>","<p>",$text);    
  20.     $text = eregi_replace("<li [^>]*>","<li>",$text);    
  21.    
  22.     // kill unwanted tags    
  23.     $text = eregi_replace("</?span[^>]*>","",$text);    
  24.     $text = eregi_replace("</?body[^>]*>","",$text);    
  25.     $text = eregi_replace("</?div[^>]*>","",$text);    
  26.     $text = eregi_replace("<\![^>]*>","",$text);    
  27.     $text = eregi_replace("</?[a-z]\:[^>]*>","",$text);    
  28.    
  29.     // kill style and on mouse* tags    
  30.     $text = eregi_replace("([ \f\r\t\n\'\"])style=[^>]+", "\\1", $text);    
  31.     $text = eregi_replace("([ \f\r\t\n\'\"])on[a-z]+=[^>]+", "\\1", $text);    
  32.    
  33.     //remove empty paragraphs    
  34.     $text = str_replace("<p></p>","",$text);    
  35.    
  36.     //remove closing </html>    
  37.     $text = str_replace("</html>","",$text);    
  38.    
  39.     //clean up white space again    
  40.     $text = eregi_replace("[[:space:]]+", " ", $text);    
  41.     $text = str_replace("> <",">\r\r<",$text);    
  42.     $text = str_replace("<br>","<br>\r",$text);    
  43. }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM