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