php Windows office转pdf

原文连接地址  http://www.javashuo.com/article/p-ufcfiuaw-ed.htmlphp

 

OpenOffice

OpenOffice 是一套开源跨平台的办公软件,由许多自由软件人士共同来维持,让你们能在 Microsoft Office 以外,还能有免费的 Office 能够使用。html

OpenOffice 与微软的办公软件套件兼容,能将 doc、xls、ppt 等文件转换为 PDF 格式,其功能绝对不比 Microsoft Office 差。java

OpenOffice 官网:http://www.openoffice.org/ajax

OpenOffice 下载:http://www.openoffice.org/download/index.html安全

OpenOffice 须要 java 支持,请确认安装了 JDK,并配置了 JRE 环境变量。服务器

 

1. 配置组件服务

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

 [组件服务] >> [计算机] >> [个人电脑] >> [DCOM配置] >> [OpenOffice Service Manager]socket

右键打开属性面板,选择安全选项卡,分别在 启动和激活权限 和 访问权限 上勾选自定义,添加 Everyone 的权限。ide

↑ 启动和激活权限 和 访问权限 都使用自定义配置工具

↑ 添加 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:该命令只须要执行一次,就能够使软件一直在后台运行,即便重启服务器也不受影响。

 

3. 配置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

 

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); } }

 

使用 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>';
}

 

 

Thinkphp中使用 

建立: 把第四步的 PDFConverter 类放进来

使用:

public function uploadFile () { $storeID = I('storeID'); $printID = I('printID'); $upload           = new \Think\Upload();// 实例化上传类
        $upload->maxSize  = 0;// 设置附件上传大小
        $upload->exts     = array ('doc','docx','xls','xlsx','ppt','pptx','jpg','gif','png','jpeg'); $upload->savePath = '/office/'; // 设置附件上传目录 // 上传文件
        $info = $upload->uploadOne($_FILES['file']); if (!$info) {// 上传错误提示错误信息
            $ajax['code'] = 'error'; $ajax['msg'] = $upload->getError(); } else {// 上传成功
            $file = $info['savepath'] . $info['savename']; Vendor('officeToPDF.officetopdf'); $converter = new \PDFConverter(); $source = dirname(dirname(dirname(__DIR__))) . '/Uploads' . $file; $export = dirname(dirname(dirname(__DIR__))) . '/Uploads/pdf/'.date('Y-m-d', time()).'/'.$info['savename']. '.pdf'; $converter->execute($source, $export); } $this->ajaxReturn($file); }
相关文章
相关标签/搜索