最近一直在研究php word文档转pdf,也在网上搜索了很多类似的资料,大多数都是通过openoffice进行转换的。
核心的代码如下:
function makepropertyvalue($name,$value,$osm){
$ostruct = $osm->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", 0, $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 '<b>source:</b> [automation bridge] <br/><b>description:</b> com.sun.star.task.errorcodeioexception: ' in i:phpstudywwwdocpreviewtest2.php on line 27
( ! ) com_exception: <b>source:</b> [automation bridge] <br/><b>description:</b> com.sun.star.task.errorcodeioexception: in i:phpstudywwwdocpreviewtest2.php on line 27
最后发现原来是转出路径的问题:通过调试得出上述代码的转出路径$output_file 是file:///i:phpstudywwwdocpreviewsdds.pdf。
然而storetourl这个方法里面需要的路径是这样的:file:///i:/phpstudy/www/docpreview/sdds.pdf。
因此只需要将$output_file的""替换为“/”
$doc_file=dirname(__file__)."/11.doc"; //源文件,doc或者wps都可以
$output_file=dirname(__file__)."/11.pdf"; //欲转pdf的文件名
$output_file=str_replace("\","/",$output_file);
$doc_file = "file:///" . $doc_file;
$output_file = "file:///" . $output_file;
$document->word2pdf($doc_file,$output_file);
以上这篇php调用openoffice实现word转pdf的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持硕编程。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!