本教程操作环境:windows7系统、php7.1版,dell g3电脑
一、配置php扩展如果是 php5.4 以前的版本,需要在 php.ini 里把 com.allow_dcom = true 打开(即去掉前面的分号)。
如果是 php5.4 之后的版本,则要在 php.ini 里增加一行扩展 extension = php_com_dotnet.dll 。
重启 apache 或 iis 服务器,打印 phpinfo() 信息,检查 com_dotnet 扩展是开启。
↑ 检查 php 的 ext 目录中 是否存在 com_dotnet.dll 文件,如果没有请自行下载对应版本的 dll
二、配置office支持openoffice 是一套开源跨平台的办公软件,由许多自由软件人士共同来维持,让大家能在 microsoft office 之外,还能有免费的 office 可以使用。
openoffice 与微软的办公软件套件兼容,能将 doc、xls、ppt 等文件转换为 pdf 格式,其功能绝对不比 microsoft office 差。
openoffice 官网:http://www.openoffice.org/
openoffice 下载:http://www.openoffice.org/download/index.html
openoffice 需要 java 支持,请确认安装了 jdk,并配置了 jre 环境变量。
1. 配置组件服务openoffice 安装完成之后,按 win+r 快捷键进入运行菜单,输入 dcomcnfg 打开组件服务。
[组件服务] >> [计算机] >> [我的电脑] >> [dcom配置] >> [openoffice service manager]
右键打开属性面板,选择安全选项卡,分别在 启动和激活权限 和 访问权限 上勾选自定义,添加 everyone 的权限。
↑ 启动和激活权限 和 访问权限 都使用自定义配置
↑ 添加 everyone 用户组,记得确认前先检查名称
↑ 两个自定义配置相同,允许 everyone 拥有所有权限
再选择标识选项卡,勾选 交互式用户,保存设置后退出。
2. 后台运行软件安装完 openoffice 后,需要启动一次确认软件可以正常运行,然后再打开命令行运行以下命令:
切换到安装目录: cd c:\program files\openoffice 4\program
后台运行该软件: soffice -headless-accept=socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard
ps:该命令只需要执行一次,就可以使软件一直在后台运行,即使重启服务器也不受影响。
三、实现文件转换pdf 转换工具(支持 doc, docx, xls, xlsx, ppt, pptx 等格式)
class pdfconverter{ private $com; /** * need to install openoffice and run in the background * soffice -headless-accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard */ public function __construct() { try { $this->com = new com('com.sun.star.servicemanager'); } catch (exception $e) { die('please be sure that openoffice.org is installed.'); } } /** * execute pdf file(absolute path) conversion * @param $source [source file] * @param $export [export file] */ public function execute($source, $export) { $source = 'file:///' . str_replace('\\', '/', $source); $export = 'file:///' . str_replace('\\', '/', $export); $this->convertprocess($source, $export); } /** * get the pdf pages * @param $pdf_path [absolute path] * @return int */ public function getpages($pdf_path) { if (!file_exists($pdf_path)) return 0; if (!is_readable($pdf_path)) return 0; 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; } return 0; } private function setproperty($name, $value) { $struct = $this->com->bridge_getstruct('com.sun.star.beans.propertyvalue'); $struct->name = $name; $struct->value = $value; return $struct; } private function convertprocess($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); }}
使用 pdfconverter(必须传入绝对路径)
$arr = array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx');$converter = new pdfconverter();foreach ($arr as $ext) { $source = __dir__ . '/office/test.' . $ext; $export = __dir__ . '/pdf/test.' . $ext . '.pdf'; $converter->execute($source, $export); echo '<p>' . $ext . ' done</p>';}
推荐学习:《php视频教程》
以上就是php怎么将work转为pdf的详细内容。
