首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用OpenOffice从我的PHP网页转换RTF到PDF?

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

Stack Overflow用户
提问于 2009-06-24 20:26:51
回答 2查看 8K关注 0票数 1

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

当我从我的PHP页面调用脚本时,页面挂起,最终显示一个错误,并且我已经注意到,我可以在Task Manager中看到soffice.bin和soffice.exe进程在我的IIS用户名下运行。

这是错误:

代码语言:javascript
复制
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用户在以下方面的权限:

代码语言:javascript
复制
C:\PHP
C:\Program Files\OpenOffice.org 3
C:\Program Files\Java
C:\WEB_ROOT ---- location for my php code

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

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

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?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对象吗?

谢谢!!

EN

回答 2

Stack Overflow用户

发布于 2009-06-25 20:42:30

耶!在这里工作的超级聪明的人的帮助下,我现在有了一个实际有效的解决方案!忘记我之前发布的所有代码,我不再使用任何代码。如果其他任何人需要在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"');

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

票数 5
EN

Stack Overflow用户

发布于 2009-06-24 21:46:51

这听起来像是open office需要与桌面交互,而且不允许这样做,因为它是由没有SERVICE_INTERACTIVE_PROCESS标志的服务调用的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1040780

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档