使用PHP + Openoffice将文档转换为PDF [英] Doc to PDF with PHP + Openoffice
问题描述
我正在尝试遵循有关使用openoffice将doc转换为pdf的教程.我有以下代码:
I am trying to follow a tutorial on converting doc to pdf using openoffice. I have the following code:
<?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/但没有运气.知道我能做什么吗?我在潮湿环境下运行,它将在生产环境下运行.
Ive tried to what this tutorial suggests: http://puno.ayun.web.id/2009/08/php-ooo-in-microsoft-windows-environment/ But no luck. Any idea what I can do? I am running this under wamp and it will be ran under wamp in production.
推荐答案
您必须具有OpenOffice安装程序才能在该计算机上作为服务运行.要简单地将odt转换为pdf,可以使用pyodconverter.他们还解释了如何设置本地OpenOffice服务: http://www.artofsolving.com/opensource/pyodconverter
You have to have OpenOffice setup to run as a service on that machine. To simply convert an odt to a pdf you can use pyodconverter. They also explain how to setup the local OpenOffice service: http://www.artofsolving.com/opensource/pyodconverter
我在编写的脚本中使用了此技术,并在此处撰写了一篇文章: http://codeuniversity.com/scripts/scr1
I'm using this technique in a script I wrote and have an article for here: http://codeuniversity.com/scripts/scr1
这篇关于使用PHP + Openoffice将文档转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!