頻道欄目
首頁 > 資訊 > php > 正文

php用windows COM組件調用openoffice接口實現word轉pdf文件時報錯的解決辦法

18-01-11        來源:[db:作者]  
收藏   我要投稿

以下是代碼:

<?php    
set_time_limit(0);    
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);    
}    
$output_dir = "c:/";    
$doc_file = "c:/t.doc";    
$pdf_file = "2.pdf";    
$output_file = $output_dir . $pdf_file;    
$doc_file = "file:///" . $doc_file;    
$output_file = "file:///" . $output_file;    
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 C:\ComsenzEXP\wwwroot\demo\index.php:17 Stack trace: #0 C:\ComsenzEXP\wwwroot\demo\index.php(17): variant->storeToURL('file:///c:/2.pd...', Array) #1 C:\ComsenzEXP\wwwroot\demo\index.php(26): word2pdf() #2 {main} thrown in C:\ComsenzEXP\wwwroot\demo\index.php on line 17 

問題解決:

$word = new COM("Word.Application") or die ("Could not initialise Object.");    
// set it to 1 to see the MS Word window (the actual opening of the document)    
$word->Visible = 0;    
// recommend to set to 0, disables alerts like "Do you want MS Word to be the default .. etc"    
$word->DisplayAlerts = 0;    
// open the word 2007-2013 document     
$word->Documents->Open('yourdocument.docx');//這個是絕對文件地址,如c:\www\1.txt這樣的地址才通過    
// save it as word 2003    
$word->ActiveDocument->SaveAs('newdocument.doc');//轉換成doc格式    
// convert word 2007-2013 to PDF    
$word->ActiveDocument->ExportAsFixedFormat('yourdocument.pdf', 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);//轉換為pdf模式    
// quit the Word process    
$word->Quit(false);    
// clean up    
unset($word);   
相關TAG標簽
上一篇:在Azure上部署Asp.NET Core Web App
下一篇:ASP.NET Core使用SkiaSharp實現驗證碼教程
相關文章
圖文推薦

關于我們 | 聯系我們 | 廣告服務 | 投資合作 | 版權申明 | 在線幫助 | 網站地圖 | 作品發布 | Vip技術培訓 | 舉報中心

版權所有: 紅黑聯盟--致力于做實用的IT技術學習網站

美女MM131爽爽爽毛片