PHP調(diào)用OpenOffice實(shí)現(xiàn)word轉(zhuǎn)PDF的方法
最近一直在研究PHP word文檔轉(zhuǎn)PDF,也在網(wǎng)上搜索了很多類似的資料,大多數(shù)都是通過(guò)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)原來(lái)是轉(zhuǎn)出路徑的問(wèn)題:通過(guò)調(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的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
thinkphp實(shí)現(xiàn)數(shù)組分頁(yè)示例
這篇文章主要介紹了thinkphp實(shí)現(xiàn)數(shù)組分頁(yè)示例,需要的朋友可以參考下2014-04-04基于curl數(shù)據(jù)采集之單頁(yè)面采集函數(shù)get_html的使用
在做數(shù)據(jù)采集時(shí)經(jīng)常要使用到curl+正則的方式采集需要的數(shù)據(jù) 根據(jù)自己的工作經(jīng)驗(yàn) 把自己寫的一些常用自定義函數(shù) 與大家來(lái)分享 如果有寫得不恰當(dāng)?shù)牡胤?請(qǐng)多多指教2013-04-04使用ob系列函數(shù)實(shí)現(xiàn)PHP網(wǎng)站頁(yè)面靜態(tài)化
php頁(yè)面緩存主要用到的是ob系列函數(shù),如ob_start(),ob_end_flush(),ob_get_contents() ,今天我們來(lái)談?wù)勈褂眠@些函數(shù)來(lái)實(shí)現(xiàn)php網(wǎng)站頁(yè)面靜態(tài)化2014-08-08Laravel-添加后臺(tái)模板AdminLte的實(shí)現(xiàn)方法
今天小編就為大家分享一篇Laravel-添加后臺(tái)模板AdminLte的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10php檢查函數(shù)必傳參數(shù)是否存在的實(shí)例詳解
這篇文章主要介紹了php檢查函數(shù)必傳參數(shù)是否存在的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-08-08Laravel框架DB facade數(shù)據(jù)庫(kù)操作詳解
這篇文章主要介紹了Laravel框架DB facade數(shù)據(jù)庫(kù)操作,結(jié)合實(shí)例形式詳細(xì)分析了laravel數(shù)據(jù)庫(kù)基本創(chuàng)建、連接、增刪改查等操作技巧,需要的朋友可以參考下2019-12-12