最近一直在研究PHP word文檔轉(zhuǎn)PDF,也在網(wǎng)上搜索了很多類似的資料,大多數(shù)都是通過OpenOffice進(jìn)行轉(zhuǎn)換的。
核心的代碼如下:
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"; //欲轉(zhuǎn)PDF的文件名 $doc_file = "file:///" . $doc_file; $output_file = "file:///" . $output_file; $document->word2pdf($doc_file,$output_file);
用上述發(fā)現(xiàn)代碼一直在報(bào)錯(cuò)
( ! ) Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> [automation bridge] <br/><b>Description:</b> com.sun.star.task.ErrorCodeIOException: ' in I:\phpStudy\WWW\DocPreview\test2.php on line 27
( ! ) com_exception: <b>Source:</b> [automation bridge] <br/><b>Description:</b> com.sun.star.task.ErrorCodeIOException: in I:\phpStudy\WWW\DocPreview\test2.php on line 27
最后發(fā)現(xiàn)原來是轉(zhuǎn)出路徑的問題:通過調(diào)試得出上述代碼的轉(zhuǎn)出路徑$output_file 是file:///I:\phpStudy\WWW\DocPreview\sdds.pdf。
然而storeToURL這個(gè)方法里面需要的路徑是這樣的:file:///I:/phpStudy/WWW/DocPreview/sdds.pdf。
因此只需要將$output_file的"\"替換為“/”
$doc_file=dirname(__FILE__)."/11.doc"; //源文件,DOC或者WPS都可以 $output_file=dirname(__FILE__)."/11.pdf"; //欲轉(zhuǎn)PDF的文件名 $output_file=str_replace("\\","/",$output_file); $doc_file = "file:///" . $doc_file; $output_file = "file:///" . $output_file; $document->word2pdf($doc_file,$output_file);
以上這篇PHP調(diào)用OpenOffice實(shí)現(xiàn)word轉(zhuǎn)PDF的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持。