前段時間負責公司內部文件平臺的設計,其中有一個需求是要可以在線瀏覽用戶上傳的 office 文件。php
個人思路是先將 office 轉換成 PDF,再經過 pdf.js 插件解析 PDF 文件,使其能在任何瀏覽器下查看。html
能夠經過 PHP 的 COM 組件,調用其它可以處理 office 文件的應用程序,利用提供的接口來轉換 PDF 文件。java
OpenOffice 是一套開源跨平臺的辦公軟件,由許多自由軟件人士共同來維持,讓你們能在 Microsoft Office 以外,還能有免費的 Office 可使用。git
OpenOffice 與微軟的辦公軟件套件兼容,能將 doc、xls、ppt 等文件轉換爲 PDF 格式,其功能絕對不比 Microsoft Office 差。github
OpenOffice 官網:http://www.openoffice.org/web
OpenOffice 下載:http://www.openoffice.org/download/index.html跨域
OpenOffice 須要 java 支持,請確認安裝了 JDK,並配置了 JRE 環境變量。瀏覽器
OpenOffice 安裝完成以後,按 win+R 快捷鍵進入運行菜單,輸入 Dcomcnfg 打開組件服務。安全
[組件服務] >> [計算機] >> [個人電腦] >> [DCOM配置] >> [OpenOffice Service Manager]服務器
右鍵打開屬性面板,選擇安全選項卡,分別在 啓動和激活權限 和 訪問權限 上勾選自定義,添加 Everyone 的權限。
↑ 啓動和激活權限 和 訪問權限 都使用自定義配置
↑ 添加 Everyone 用戶組,記得確認前先檢查名稱
↑ 兩個自定義配置相同,容許 Everyone 擁有全部權限
再選擇標識選項卡,勾選 交互式用戶,保存設置後退出。
安裝完 OpenOffice 後,須要啓動一次確認軟件能夠正常運行,而後再打開命令行運行如下命令:
切換到安裝目錄: cd C:\Program Files\OpenOffice 4\program
後臺運行該軟件: soffice -headless-accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
PS:該命令只須要執行一次,就可使軟件一直在後臺運行,即便重啓服務器也不受影響。
若是是 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
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>'; }
最後分享一個基於 HTML5 的 PDF 閱讀器插件 pdf.js,它是 Mozilla 實驗室在 GitHub 上開源的一款 js 庫,專門用來讀取 PDF 文件。
因爲是 Mozilla 的產品,因此在 Firefox 下表現的十分出色,而且只要是支持 HTML5 的瀏覽器,都能使用這款閱讀器。
項目地址:https://github.com/mozilla/pdf.js
插件下載:http://mozilla.github.io/pdf.js/
↑ pdf.js 不能打開本地 pdf 文件,但能夠經過 url 打開服務器上的文件,不支持跨域瀏覽 pdf
使用方法:1)將插件解壓,放置在網站的根目錄;2)經過網址訪問 viewer.html;3)添加 file 參數指定 pdf 路徑;
例如:http://localhost/pdfjs/web/viewer.php?file=/office/example.pdf