windows系统 安装OpenOffice(office文档转成PDF文件)

摘要:最近在做文件管理系统的时候,需要将office文档转成PDF文件,于是想到调用OpenOffice软件的com组件去处理

1.首先是下载OpenOffice安装包(官网下载需要翻墙,推荐云墙,购买流量下载还是蛮快的)

        云盘:https://pan.baidu.com/s/1i43GzPV

2.配置组件服务

        A.OpenOffice 安装完成之后,按 win+R 快捷键进入运行菜单,输入 Dcomcnfg 打开组件服务

        B.

        C.右键点击属性选择安全,下面都选择自定义

然后选择前两个的编辑,分别选中所有权限


        D.确定后选择标识,并将用户账号选择为 交互式用户.


3.使用命令后台启动服务(只需要执行一次,就可以使软件一直在后台运行,即使重启服务器也不受影响)

    首先需要找到你的安装位置(在C盘搜索soffice.exe文件,发现在C:\Program Files\OpenOffice 4\program)

    所以先进入项目位置c:

                                    cd Program Files\OpenOffice 4\program

                                    输入命令:soffice -headless-accept="socket,host=127.0.0.1,port=8100;urp;"-nofirststartwizard启动

4.在php.ini中开启com_dotnet扩展(我用的PHP7),添加extension=php_com_dotnet.dll,重启服务器后


现在我们就可以去新建一个test.php文件进行测试了

5.代码如下:

classPDFConverter

{

private$com;

/**

* 需要安装openoffice并在后台运行

* soffice -headless-accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

*/

public function__construct()

{

try{

$this->com=newCOM('com.sun.star.ServiceManager');

}catch(Exception$e) {

die('Please be sure that OpenOffice.org is installed.');

}

}

/**

* 执行PDF文件(绝对路径)转换

*@param$source [source file]

*@param$export [export file]

*/

public functionexecute($source,$export)

{

$source='file:///'.str_replace('\\','/',$source);

$export='file:///'.str_replace('\\','/',$export);

$this->convertProcess($source,$export);

}

/**

* 获取PDF页面

*@param$pdf_path [absolute path]

*@returnint

*/

public functiongetPages($pdf_path)

{

if(!file_exists($pdf_path))return0;

if(!is_readable($pdf_path))return0;

if($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);

return$page;

}

return0;

}

private functionsetProperty($name,$value)

{

$struct=$this->com->Bridge_GetStruct('com.sun.star.beans.PropertyValue');

$struct->Name=$name;

$struct->Value=$value;

return$struct;

}

private functionconvertProcess($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);

$program->close(true);

}

}

//$arr = array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx');

$converter=newPDFConverter();

$source=__DIR__.'/office/test.'.'pptx';//转换前文件(绝对路径)

$export=__DIR__.'/pdf/test.'.'pptx'.'.pdf';//转换后文件(绝对路径)

$converter->execute($source,$export);



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章