PHP调用OpenOffice实现word转PDF的方法

最近一直在研究PHP word文档转PDF,也在网上搜索了很多类似的资料,大多数都是通过OpenOffice进行转换的。

核心的代码如下:

Bridge_GetStruct("com.sun.star.beans.PropertyValue"); $oStruct->Name = $name; $oStruct->Value = $value; return $oStruct; }

function word2pdf($doc_url,$output_url){
$osm = new COM("com.sun.star.ServiceManager") or die ("Please be sure that OpenOffice.org is installed.n");
$args = array(MakePropertyValue("Hidden",true,$osm));
$oDesktop = $osm->createInstance("com.sun.star.frame.Desktop");
$oWriterDoc = $oDesktop->loadComponentFromURL($doc_url,"_blank",$args);
$export_args = array(MakePropertyValue("FilterName","writer_pdf_Export",$osm));
$oWriterDoc->storetoURL($output_url,$export_args);
$oWriterDoc->close(true);
}

$doc_file=dirname(FILE)."/11.doc"; //源文件,DOC或者WPS都可以
$output_file=dirname(FILE)."/11.pdf"; //欲转PDF的文件
$doc_file = "file:///" . $doc_file;
$output_file = "file:///" . $output_file;
$document->word2pdf($doc_file,$output_file);

用上述发现代码一直在报错

( ! ) Fatal error: Uncaught exception 'com_exception' with message 'Source: [automation bridge]
Description: com.sun.star.task.ErrorCodeIOException: ' in I:\PHPStudy\WWW\DocPreview\test2.PHP on line 27

( ! ) com_exception: Source: [automation bridge]
Description: com.sun.star.task.ErrorCodeIOException: in I:\PHPStudy\WWW\DocPreview\test2.PHP on line 27

最后发现原来是转出路径的问题:通过调试得出上述代码的转出路径$output_file 是file:///I:\PHPStudy\WWW\DocPreview\sdds.pdf。

然而storetoURL这个方法里面需要的路径是这样的:file:///I:/PHPStudy/WWW/DocPreview/sdds.pdf。

因此只需要将$output_file的"\"替换为“/”

word2pdf($doc_file,$output_file);

以上这篇PHP调用OpenOffice实现word转PDF的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...