2011-01-10 60 views
2

我正在尝试使用openoffice将doc转换为pdf。我有以下代码:使用PHP + Openoffice处理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){ 
    // Invoke the OpenOffice.org service manager 
    $osm = new COM("com.sun.star.ServiceManager") or die ("Please be sure that OpenOffice.org is installed.\n"); 
    // Set the application to remain hidden to avoid flashing the document onscreen 
    $args = array(MakePropertyValue("Hidden",true,$osm)); 
    // Launch the desktop 
    $top = $osm->createInstance("com.sun.star.frame.Desktop"); 
    // Load the .doc file, and pass in the "Hidden" property from above 
    $oWriterDoc = $top->loadComponentFromURL($doc_url,"_blank", 0, $args); 
    // Set up the arguments for the PDF output 
    $export_args = array(MakePropertyValue("FilterName","writer_pdf_Export",$osm)); 
    // Write out the PDF 
    $oWriterDoc->storeToURL($output_url,$export_args); 
    $oWriterDoc->close(true); 
} 
$output_dir = './'; 
$doc_file = './test.docx'; 
$pdf_file = 'DpmR5Reqv1.20.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 'Failed to create COM object `com.sun.star.ServiceManager' in C:\wamp\www\Projects\doc_to_pdf\index.php on line 11 
(!) com_exception: Failed to create COM object `com.sun.star.ServiceManager': Invalid syntax in C:\wamp\www\Projects\doc_to_pdf\index.php on line 11 

香港专业教育学院尝试一下这个教程提示:http://puno.ayun.web.id/2009/08/php-ooo-in-microsoft-windows-environment/,但没有运气。任何想法我可以做什么?我正在用这种方式运行,它将在生产中运行。

+0

哦,上帝,当然有一个更简单的方法来将.doc转换为PDF,而不是OpenOffice。查看AntiWord。您可以通过命令行非常简单地从doc转换为PDF – Earlz 2011-01-11 03:26:13

回答

0

请您目录中安装开放式办公。 OpenOffice安装程序在该机器上作为服务运行。