PHP 实现 word/excel/ppt 转换为 PDF
2017-06-15 02:58
495 查看
前段时间负责公司内部文件平台的设计,其中有一个需求是要能够在线浏览用户上传的 office 文件。
我的思路是先将 office 转换成 PDF,再通过 pdf.js 插件解析 PDF 文件,使其能在任何浏览器下查看。
可以通过 PHP 的 COM 组件,调用其它能够处理 office 文件的应用程序,利用提供的接口来转换 PDF 文件。
OpenOffice
OpenOffice 是一套开源跨平台的办公软件,由许多自由软件人士共同来维持,让大家能在 Microsoft Office 之外,还能有免费的 Office 可以使用。OpenOffice 与微软的办公软件套件兼容,能将 doc、xls、ppt 等文件转换为 PDF 格式,其功能绝对不比 Microsoft Office 差。
OpenOffice 官网:http://www.openoffice.org/
OpenOffice 下载:http://www.openoffice.org/download/index.html
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.php
使用 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>'; }
5. 查看PDF文档
最后分享一个基于 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
相关文章推荐
- Java 使用jacob实现各类办公文档(ppt,Excel,word,text,imge)转换成PDF
- PDF预览之word、excel、ppt转换为pdf,PHP内通过exec()调用笔记.txt
- PHP实现Word与excel等转换pdf方法
- Ubuntu 服务器 PHP实现word、excel、ppt、pdf 等文档在线阅读功能的实现
- PHP 实现Word,excel等转换pdf
- PHP 实现Word,excel等转换pdf
- 利用wps的API将word、excel、ppt转换为pdf
- c#实现word,excel转pdf代码及部分Office 2007文件格式转换为xps和pdf代码整理
- asp.net实现word、excel、ppt、pdf在线预览
- C#.net word excel powerpoint (ppt) 转换成 pdf
- Asp.net MVC 利用(aspose+pdfobject.js) 实现在线预览word、excel、ppt、pdf文件
- Java通过OpenOffice将word、ppt、excel、图片、txt文档转换成PDF
- Saving Workbooks to PDF and XPS Formats in Excel 2007 (C#.net word excel powerpoint (ppt) 转换成 pdf 文件)
- OFFICE(word/ppt/excel)转换成HTML实现
- .NET WEB 实现office文档(word、excel、ppt)转PDF
- PHP实现仿百度文库,豆丁在线文档效果(word,excel,ppt转flash)
- 使用WPS的API转换word、excel、ppt为PDF问题
- java实现word,ppt,excel,jpg转pdf
- 将word、excel、ppt、html、txt,pdf转换成图片源代码
- 实现Word、Excel、Txt文档在线转换为Swf(或PDF)