PHP Windows系统下调用OpenOffice

项目需要把用户上传的word文档转换为pdf文件,方便用户浏览。经过谷歌百度找到PHP可以使用COM组件调用微软的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 }