以下是代码:
<?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);
参考: http://blog.csdn.net/jaray/article/details/52690620