1

我的操作系统是 Windows XP,我使用的是 IIS 5.1 和 PHP 5.2.9。我正在尝试从我的 PHP 页面调用 PHP 脚本,以便使用 OpenOffice 将 RTF 文档转换为 PDF。当我直接从命令行调用它时,该脚本工作得很好,但是当我从我的 PHP 网页启动时,我没有成功让相同的脚本工作。

当我从我的 PHP 页面调用脚本时,页面挂起,最终显示错误,我注意到我可以在任务管理器中看到 soffice.bin 和 soffice.exe 进程在我的 IIS 用户名下运行。

这是错误:

Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `com.sun.star.ServiceManager': Server execution failed ' in C:\WEB_ROOT\SoftwareContract\WordToPdf.php:14 Stack trace: #0 C:\WEB_ROOT\SoftwareContract\WordToPdf.php(14): com->com('com.sun.star.Se...') #1 C:\WEB_ROOT\SoftwareContract\Index.php(11): word2pdf('file:///C:/web_...', 'file:///C:/web_...') #2 {main} thrown in C:\WEB_ROOT\SoftwareContract\WordToPdf.php on line 14

我已经在这些领域仔细检查了我的 IIS 用户的权限:

C:\PHP
C:\Program Files\OpenOffice.org 3
C:\Program Files\Java
C:\WEB_ROOT ---- location for my php code

在每种情况下,我的 IIS 用户都具有以下权限:读取和执行、列出文件夹内容、读取。并且在每种情况下,都没有检查“拒绝”来抵消权限。我还授予 IIS 用户对 php 代码所在的 Web_Root 文件夹的写入权限。

这是调用转换函数 WordToPdf 的 php:

<?php
require_once('WordToPdf.php');

$output_dir = 'C:/web_root/softwarecontract/';
$doc_file = 'C:/web_root/softwarecontract/testdoc.rtf';
$pdf_file = 'output.pdf';
$output_file = $output_dir . $pdf_file;
$doc_file = "file:///" . $doc_file;
$output_file = "file:///" . $output_file;
word2pdf($doc_file,$output_file);
?>

这是 WordToPdf.php:

<?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 
    $oDesktop = $osm->createInstance("com.sun.star.frame.Desktop");
    //Load the .doc file, and pass in the "Hidden" property from above
    $oWriterDoc = $oDesktop->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);
}
?>

我的权限是否有任何问题,或者我需要检查 IIS 用户权限的任何其他区域?如果不是权限问题,有谁知道为什么 IIS 无法创建 COM 对象?

谢谢!!

4

2 回答 2

5

耶!!!!在工作中的超级笨蛋的帮助下,我现在有了一个真正可行的解决方案!忘记我之前发布的所有代码,我不再使用它了。如果其他人需要在 IIS 上将 Word 文档从 PHP 转换为 PDF,则可以使用以下方法:

1)当然安装OpenOffice

2)去这里:
http://www.artofsolving.com/opensource/pyodconverter,下载 DocumentConverter.py

3) 将 DocumentConverter.py 放在 soffice.exe 所在的同一个 OpenOffice 文件夹中,可能是 C:\Program Files\OpenOffice.org 3\program

4) 将 OpenOffice 作为服务启动:

a)您可以从命令行执行此操作:

soffice "-accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager" -headless -norestore -nologo -nofirststartwizard

b) 或按照本网站上的说明,使用适当的参数将 OpenOffice 设置为 Windows 服务:http://www.artofsolving.com/node/10

5)从PHP运行这个命令:

exec('python "C:\Program Files\OpenOffice.org 3\program\DocumentConverter.py" path_to_doc\test.doc path_to_output_pdf\test.pdf"');

是的,多么整洁漂亮的解决方案!

于 2009-06-25T20:42:30.470 回答
0

这听起来像是开放式办公室需要与桌面交互并且不允许这样做,因为它是由没有SERVICE_INTERACTIVE_PROCESS标志的服务调用的。

于 2009-06-24T21:46:50.537 回答