首页 > 程序开发 > Web开发 > php >

php用windows COM组件调用openoffice接口实现word转pdf文件时报错的解决办法

2018-01-11

php用windows COM组件调用openoffice接口实现word转pdf文件时报错的解决办法。

以下是代码:

<?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 &#39;com_exception&#39; with message &#39;<b>Source:</b> [automation bridge] <br/><b>Description:</b> com.sun.star.task.ErrorCodeIOException: &#39; in C:\ComsenzEXP\wwwroot\demo\index.php:17 Stack trace: #0 C:\ComsenzEXP\wwwroot\demo\index.php(17): variant->storeToURL(&#39;file:///c:/2.pd...&#39;, 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(&#39;yourdocument.docx&#39;);//这个是绝对文件地址,如c:\www\1.txt这样的地址才通过    
// save it as word 2003    
$word->ActiveDocument->SaveAs(&#39;newdocument.doc&#39;);//转换成doc格式    
// convert word 2007-2013 to PDF    
$word->ActiveDocument->ExportAsFixedFormat(&#39;yourdocument.pdf&#39;, 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);   
相关文章
最新文章
热点推荐