PHP将word/excel/ppt转PDF最常用的方法是使用OpenOffice进行转换。我们来看看实现的核心代码:classPDFConverter{private$com;/***需要安装openoffice并在后台运行*soffice-headless-accept="socket,host=127.0.0.1,port=8100;urp;"-nofirststartwizard*/publicfunction__construct(){try{$this->com=newCOM('com.sun.star.ServiceManager');}catch(Exception$e){die('请确保已安装OpenOffice.org。');}}/***执行PDF文件(绝对路径)转换*@param$source[源文件]*@param$export[导出文件]*/publicfunctionexecute($source,$export){$source='file///'。str_replace('\\','/',$source);$export='文件:///'。str_replace('\\','/',$export);$this->convertProcess($source,$export);}/***获取PDF页面*@param$pdf_path[绝对路径]*@returnint*/publicfunctiongetPages($pdf_path){if(!file_exists($pdf_path))返回0;如果(!is_readable($pdf_path))返回0;如果($fp=fopen($pdf_path,'r')){$page=0;while(!feof($fp)){$line=fgets($fp,255);if(preg_match('/\/Count[0-9]+/',$line,$matches)){preg_match('/[0-9]+/',$matches[0],$matches2);$page=($page<$matches2[0])?$matches2[0]:$page;}}fclose($fp);返回$页面;}返回0;}privatefunctionsetProperty($name,$value){$struct=$this->com->Bridge_GetStruct('com.sun.star.beans.PropertyValue');$struct->名称=$名称;$struct->Value=$value;返回$结构;}privatefunctionconvertProcess($source,$export){$desktop_args=array($this->setProperty('Hidden',true));$desktop=$this->com->createInstance('com.sun.star.frame.Desktop');$export_args=array($this->setProperty('FilterName','writer_pdf_Export'));$program=$desktop->loadComponentFromURL($source,'_blank',0,$desktop_args);$program->storeToURL($export,$export_args);$程序->关闭(真);}}更多详情,请关注公众号,回复word获取word相关信息。
