1 <?php2 /**3 * office文档转换类4 * 实现office任何格式文档网页浏览5 * author hui6 * 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 = true12 * php.ini 添加extension=php_com_dotnet.dll13 * 检查该文件14 * php/ext/php_com_dotnet.dll15 */
16
17 classConvert{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 int48 */
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
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 array89 */
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 }