PHP Windows系統下調用OpenOffice


項目需要把用戶上傳的word文檔轉換為pdf文件,方便用戶瀏覽。經過谷歌百度找到PHP可以使用COM組件調用微軟的openoffice來實現文檔轉換

 

1,安裝OpenOffice

  •   安裝OpenOffice 4.1.3 (zh-CN),可百度直接下載對應操作系統版本

2,設置權限

  •   cmd 運行Dcomcnfg.exe->組件服務->計算機->我的電腦->DCOM配置->OpenOffice Service Manager
  •   鼠標右擊->屬性,選擇安全 ,和標識這2個配置。標識配置=>交互式用戶,安全=>自定義,全部添加Everyone權限。
  •   點擊編輯->添加Everyone權限就行了
  •   啟動OpenOffice服務命令: 
  •   打開cmd(建議用管理員權限運行,保證服務正常開啟)。 先進入OpenOffice安裝目錄,例如我安裝的: 
  •   cd  C:\Program Files (x86)\OpenOffice 4\program
  •        啟動服務:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

3,配置php.ini

  •   php.ini 開啟 com.allow_dcom = true
  •   php.ini 添加 extension=php_com_dotnet.dll
  •   檢查該文件 php/ext/php_com_dotnet.dll

下面奉上我整理的代碼,希望能幫到你

  1 <?php
  2 /**
  3  * office文檔轉換類
  4  * 實現office任何格式文檔網頁瀏覽
  5  * author hui
  6  * 1,安裝OpenOffice 4.1.3 (zh-CN)
  7  * 
  8  * 2,安裝 SWFTOOLS http://www.swftools.org/download.html到C盤
  9  *   並把pdf2swf.exe文件移動到C盤根目錄
 10  *
 11  * 3,php.ini 開啟com.allow_dcom = true
 12  *   php.ini 添加extension=php_com_dotnet.dll
 13  *   檢查該文件
 14  *   php/ext/php_com_dotnet.dll
 15  */
 16 
 17 class Convert{
 18     
 19     private $osm;
 20     
 21     // 構造函數,啟用OpenOffice的COM組件
 22     public function __construct(){
 23         ini_set('magic_quotes_runtime', 0); // 設置運行時間
 24         $this->osm = new COM("com.sun.star.ServiceManager") or die("Please be sure that OpenOffice.org is installed.n");
 25     }
 26     
 27     private function MakePropertyValue($name, $value){
 28         $oStruct = $this->osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue");
 29         $oStruct->Name = $name;
 30         $oStruct->Value = $value;
 31         return $oStruct;
 32     }
 33     
 34     private function transform($input_url, $output_url){
 35         $args = array($this->MakePropertyValue('Hidden', true));
 36         $oDesktop = $this->osm->createInstance("com.sun.star.frame.Desktop");
 37         $oWriterDoc = $oDesktop->loadComponentFromURL($input_url, '_blank', 0, $args);
 38         $export_args = array($this->MakePropertyValue('FilterName', 'writer_pdf_Export'));
 39         $oWriterDoc->storeToURL($output_url, $export_args);
 40         $oWriterDoc->close(true);
 41         return $this->getPdfPages($output_url);
 42     }
 43 
 44     /**
 45      * getPdfPages 獲取PDF文件頁數的函數獲取,文件應當對當前用戶可讀(linux下)
 46      * @param  string $path 文件路徑
 47      * @return int
 48      */
 49     private function getPdfPages($path = ''){
 50         if(!file_exists($path)) return 0;
 51         if(!is_readable($path)) return 0;
 52         $fp=@fopen($path, "r"); // 打開文件
 53         if(!$fp){
 54             return 0;
 55         }else{
 56             $max = 0;
 57             while(!feof($fp)){
 58                 $line = fgets($fp,255);
 59                 if(preg_match('/\/Count [0-9]+/', $line, $matches)){
 60                     preg_match('/[0-9]+/', $matches[0], $matches2);
 61                     if ($max<$matches2[0]) $max = $matches2[0];
 62                 }
 63             }
 64             fclose($fp);
 65             return $max; // 返回頁數
 66         }
 67     }
 68 
 69     /**
 70      * office文件轉換pdf格式
 71      * @param  string $input  需要轉換的文件
 72      * @param  string $output 轉換后的pdf文件
 73      * @return return string 頁數
 74      */
 75     public function run($input = '', $output = ''){
 76         if(empty($input) || empty($output)){
 77             return ['error' => 1, 'msg' => '參數缺失', 'flag' => 'run'];
 78         }
 79         $input = "file:///" . str_replace("\\", "/", $input);
 80         $output = "file:///" . str_replace("\\", "/", $output);
 81         return $this->transform($input, $output);
 82     }
 83 
 84     /**
 85      * pdf2swf pdf文件轉換swf格式
 86      * @param  string $word_file  需要轉換的文件路徑
 87      * @param  string $attach_dir 保存文件地址
 88      * @return array
 89      */
 90     public function pdf2swf($word_file = '', $attach_dir = ''){
 91         if(empty($word_file) || empty($attach_dir)){
 92             return ['error' => 1, 'msg' => '參數缺失', 'flag' => 'pdf2swf'];
 93         }
 94         $file_name = uniqid();
 95         $pdf_file =  "{$attach_dir}{$file_name}.pdf";                                         // PDF文件絕對路徑
 96         $page = $this->run($word_file, $pdf_file);                                             // 文件先轉換為PDF格式
 97         if(isset($page) && $page > 0){
 98             $swf_file = "{$attach_dir}{$file_name}.swf";                                     // 轉換后的swf文件
 99             $pd = str_replace("/", "\\", $pdf_file);
100             $sw = str_replace("/", "\\", $swf_file);
101             $cmd = Config::get('websetup.swftools') . " -t {$pd} -s flashversion=9 -o {$sw}";
102             $phpwsh = new COM("Wscript.Shell") or die("Create Wscript.Shell Failed!");
103             $exec = $phpwsh->exec("cmd.exe /c" . $cmd);                                     // cmd執行pdf2swf轉換命令
104             $stdout = $exec->stdout();
105             $stdout->readall();
106             if(is_file($sw)){                                                                 // swf文件
107                 if(is_file($pdf_file)){                                                     // 刪除pdf文件
108                     unlink($pdf_file);
109                 }
110                 return ['error' => 0, 'page' => $page, 'swf_file' => $file_name];
111             }else{
112                 return ['error' => 1, 'msg' => 'swf文件不存在', 'flag' => 'pdf2swf'];
113             }
114         }else{
115             return ['error' => 1, 'msg' => '轉換pdf失敗', 'flag' => 'pdf2swf'];
116         }
117     }
118     
119 }

 


免責聲明!

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



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