PHP 通过 Com 组件实现 Office 文件转 PDF
最近做项目,客户要求在内网情况下(有可能没网),对 Office 文件进行预览,直接排除使用微软提供的解析服务,试了下 PhpOffice 的三件套,想转成 PDF 格式,发现 PPT 文件无法转换(也可能是自己没有找到方法),最后只能使用老办法:通过 COM 组件调用 Office 文件的应用程序,利用提供的接口转换成 PDF 文件完成预览。
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] 组并允许其拥有所有权限。
再选择 [标识] 选项卡,勾选 [交互式用户],应用设置后退出。
2、后台运行软件
安装完 OpenOffice 后,需要启动一次确认软件可以正常运行,然后再打开命令行运行以下命令:
cd C:\Program Files\OpenOffice 4\program \\切换到安装目录
soffice -headless-accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard \\后台运行该软件
PS:该命令只需要执行一次,就可以使软件一直在后台运行,即使重启服务器也不受影响。
3、配置PHP扩展
打开 php.ini 文件,开启 com.allow_dcom = true (即去掉前面的分号);
在 php.ini 里增加一行扩展:extension = php_com_dotnet.dll;
检查 php 的 ext 目录中 是否存在 com_dotnet.dll 文件,如果没有请自行下载对应版本的 dll;
重启 Apache 或 IIS 服务器,打印 phpinfo() 信息,检查 com_dotnet 扩展是开启。
4、实现文件转换
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);
}
}
可以先测试 Com 组件是否可调用:
$obj = new COM('com.sun.star.ServiceManager') or die('Unable to instanciate Word');
进行转换(文件必须是绝对路径):
$converter = new PDFConverter(); //如果是 Thinkphp,注意写成 new /PDFConverter();
$source = __DIR__ . '/data/test.docx';
$export = __DIR__ . '/data/test.pdf';
$converter->execute($source, $export); //查看 pdf 文件是否生成
5、查看 PDF 文档
大部分浏览器都可以使用插件直接打开 PDF 文件,为了避免出现直接下载或者想做某些隐藏的情况,可以使用基于 HTML5 的 PDF 阅读器插件 pdf.js,只要支持 HTML5 的浏览器,都可以使用。
项目地址:https://github.com/mozilla/pdf.js
插件下载:http://mozilla.github.io/pdf.js
使用方法:
1)将插件解压,放置在网站的根目录;
2)通过网址访问 viewer.html;
3)添加 file 参数指定 pdf 路径 (http://localhost/pdfjs/web/viewer.html?file=/data/test.pdf);
注意:pdf.js 不能打开本地 pdf 文件,但可以通过 url 打开服务器上的文件,不支持跨域浏览 pdf。
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
评论已关闭