PHP 实现 word/excel/ppt 转换为 PDF

一般最常见的就是利用OpenOffice来转换,来看看实现的核心代码:


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

更多详细细节可以关注公众号,并回复 word 获取word相关的资料。

原文地址:https://segmentfault.com/a/1190000016861774

word/excel/ppt 2 PDF的更多相关文章

  1. word,excel,ppt转Pdf,Pdf转Swf,通过flexpaper+swftools实现在线预览

    其实这是我好几年前的项目,现在再用这种方式我也不建议了,毕竟未来flash慢慢会淘汰,此方式也是因为目测大部分人都装了flash,才这么做的,但是页面展示效果也不好.其实还是考虑收费的控件,毕竟收费的 ...

  2. word,excel,ppt转pdf

    第一步 需要下载jar包和jacob-1.14.3-x64.dll * <dependency> * <groupId>net.sf.jacob-project</gro ...

  3. PHP 实现 word/excel/ppt 转换为 PDF

    前段时间负责公司内部文件平台的设计,其中有一个需求是要能够在线浏览用户上传的 office 文件. 我的思路是先将 office 转换成 PDF,再通过 pdf.js 插件解析 PDF 文件,使其能在 ...

  4. 21.PHP实现Word/Excel/PPT转换为PDF

    参考文档: https://www.cnblogs.com/woider/p/7003481.html http://blog.csdn.net/aoshilang2249/article/detai ...

  5. PDF/WORD/EXCEL/PPT 文档在线阅读

    查资料看了2种解决方法: 1.通过办公软件dll转换,用flans去看 2.通过Aspose转换成pdf格式,在用js前台读pdf(我用的pdf.js) 今天我解决的就是WORD/EXCEL/PPT ...

  6. Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结

    Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word  excel pdf 的web预览要求 ...

  7. 在线文档转换API word,excel,ppt等在线文件转pdf、png

    在线文档转换API提供word,excel,ppt等在线文件转pdf.png等,文档:https://www.juhe.cn/docs/api/id/259 接口地址:http://v.juhe.cn ...

  8. java 如何将 word,excel,ppt如何转pdf --openoffice (1)

    承上启下,可折叠 上一篇说的是:服务器是windows server时,用jacob将msoffice(指的是word,excel,ppt)转换成pdf. 若被部署项目的服务器是centOS等linu ...

  9. 微信小程序云开发-云存储-下载并打开文件文件(word/excel/ppt/pdf)

    一.wxml文件 1.写文本框,用来获取文件链接. 2.按钮,点击下载文件 <!-- 下载文件(word/excel/ppt/pdf等) --> <view class=" ...

随机推荐

  1. HD-ACM算法专攻系列(4)——A == B ?

    题目描述: 源码: /**/ #include"iostream" #include"string" using namespace std; string S ...

  2. ASP调用WebService转化成JSON数据,附json.min.asp

    首先定义SOAP数据,然后创建HTTP对象,然后使用POST提交,获取状态码为200,就说明调用成功,再进行下一步操作…… <!--#Include virtual="/Include ...

  3. 接口测试工具篇--postman

    上面是postman的主页面,可以输入接口url,设置请求方式(get或post) 下面主要讲一些其他接口测试需要用到的设置Authorization用来设置权限验证的,可以选择权限验证的方式 hea ...

  4. [SDOI2008]沙拉公主的困惑 线性筛_欧拉函数_逆元_快速幂

    Code: #include<cstdio> using namespace std; typedef long long ll; const int maxn=10000000+1; l ...

  5. vue非空校验

    效果图 实现代码 //页面html <div> <ul class="listinfo"> <li> <span class=" ...

  6. 正则表达式中的/i

    i = insensitive means case-insensitive 表示大小写不敏感

  7. ElementUi rules表单验证

    ElementUi 表单验证 工作中常用到的JS验证 可以在pattern中书写正则,并且配合elementUI进行表单验证. pattern 属性规定用于验证输入字段的模式.模式指的是正则表达式. ...

  8. Qt之图形(组合)

    简述 使用QPainter绘制图形或者图像时,在重叠区域使用组合模式(Composition_mode).在绘图设备上通过组合模式使用QImage时,必须使用Format_ARGB32_Premult ...

  9. Android Studio打包.so文件教程

    在eclipse里,.so文件eclipse会帮助我们自动打包进apk文件,通常是放在:libs/armeabi目录,然后把libxxx.so拷贝到这个目录下,这样NDK就会自动把这个libxxx.s ...

  10. spring 、Mybatis配置sql server数据库

    学习了:http://www.cnblogs.com/amwicfai/p/3181003.html beans.xml中的配置写法: <!-- 数据源 --> <bean id=& ...