频道栏目
首页 > 资讯 > 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('../201510/GridViewLiveTiles.html', 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技术学习网站