如何使用OpenOffice从我的PHP网页将RTF转换为PDF?

时间:2021-02-12 01:35:33

My OS is Windows XP, and I'm using IIS 5.1 and PHP 5.2.9. I'm trying to call a PHP script from my PHP page in order to convert an RTF doc to a PDF, using OpenOffice. The script works just fine when I call it directly from the command line, but I'm not having any success getting the very same script to work when launched from my PHP web page.

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

When I call the script from my PHP page, the page hangs, eventually showing an error, and I've noted that I can see in the Task Manager where the soffice.bin and soffice.exe processes are running under my IIS user name.

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

This is the error:

这是错误:

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

I've double-checked permissions for my IIS user in these areas:

我在这些方面仔细检查了我的IIS用户的权限:

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

In each case, my IIS user has these permissions: Read and Execute, List Folder Contents, Read. And in each case there are no "denys" checked to offset the permissions. I also gave the IIS user write permissions for the Web_Root folder where the php code resides.

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

This is the php which calls the conversion function, WordToPdf:

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

<?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);
?>

And this is WordToPdf.php:

这是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);
}
?>

Are there any problems with my permissions, or any additional areas where I need to check permissions for the IIS user? Does anyone have an idea why IIS failed to create the COM object, if not a permissions issue?

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

Thanks!!

2 个解决方案

#1


Yay!!!! With the help of a super-duper brainiac here at work, I now have a solution that actually WORKS! Forget all the code I posted earlier, I'm not using any of it anymore. If anyone else needs to convert word docs to PDF from PHP on IIS here's what will work:

好极了!!!!在这里工作的超级duper brainiac的帮助下,我现在有一个实际工作的解决方案!忘记我之前发布的所有代码,我不再使用它了。如果其他人需要在IIS上将PHP文档转换为PDF,那么这将是什么工作:

1) Install OpenOffice, of course

1)当然安装OpenOffice

2) Go here:
http://www.artofsolving.com/opensource/pyodconverter, and download DocumentConverter.py

2)转到此处:http://www.artofsolving.com/opensource/pyodconverter,并下载DocumentConverter.py

3) Place DocumentConverter.py in the same OpenOffice folder where soffice.exe resides, probably C:\Program Files\OpenOffice.org 3\program

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

4) Start OpenOffice as a service:

4)启动OpenOffice作为服务:

a)You can do this from the command line:

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

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

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

b) Or follow the directions on this website and setup OpenOffice as a windows service with the proper parameters: http://www.artofsolving.com/node/10

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

5) From PHP run this command:

5)从PHP运行此命令:

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

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

Yay, what a neat and pretty solution!

是的,这是一个多么漂亮和漂亮的解决方案!

#2


That sounds like open office needs to interact with the desktop and isn't allowed to since it was invoked by a service without the SERVICE_INTERACTIVE_PROCESS flag.

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

#1


Yay!!!! With the help of a super-duper brainiac here at work, I now have a solution that actually WORKS! Forget all the code I posted earlier, I'm not using any of it anymore. If anyone else needs to convert word docs to PDF from PHP on IIS here's what will work:

好极了!!!!在这里工作的超级duper brainiac的帮助下,我现在有一个实际工作的解决方案!忘记我之前发布的所有代码,我不再使用它了。如果其他人需要在IIS上将PHP文档转换为PDF,那么这将是什么工作:

1) Install OpenOffice, of course

1)当然安装OpenOffice

2) Go here:
http://www.artofsolving.com/opensource/pyodconverter, and download DocumentConverter.py

2)转到此处:http://www.artofsolving.com/opensource/pyodconverter,并下载DocumentConverter.py

3) Place DocumentConverter.py in the same OpenOffice folder where soffice.exe resides, probably C:\Program Files\OpenOffice.org 3\program

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

4) Start OpenOffice as a service:

4)启动OpenOffice作为服务:

a)You can do this from the command line:

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

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

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

b) Or follow the directions on this website and setup OpenOffice as a windows service with the proper parameters: http://www.artofsolving.com/node/10

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

5) From PHP run this command:

5)从PHP运行此命令:

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

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

Yay, what a neat and pretty solution!

是的,这是一个多么漂亮和漂亮的解决方案!

#2


That sounds like open office needs to interact with the desktop and isn't allowed to since it was invoked by a service without the SERVICE_INTERACTIVE_PROCESS flag.

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