使用 PHP 将文档转换为 PDF +开放办公
我正在尝试遵循使用 openoffice 将 doc 转换为 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/ 但运气不佳。知道我能做什么吗?我在 wamp 下运行它,并且在生产中它将在 wamp 下运行。
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);
?>
I get the error:
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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须安装 OpenOffice 才能作为该计算机上的服务运行。要简单地将 odt 转换为 pdf,您可以使用 pyodconverter。他们还解释了如何设置本地 OpenOffice 服务:
http://www.artofsolving.com/opensource/pyodconverter
我正在使用这种技术我写的脚本,这里有一篇文章:
http://codeuniversity.com/scripts/scr1
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
I'm using this technique in a script I wrote and have an article for here:
http://codeuniversity.com/scripts/scr1
请在您的目录中安装 Open Office。 OpenOffice 设置为在该计算机上作为服务运行。
Please install Open Office in your Directory. OpenOffice setup to run as a service on that machine.
使用无头 libreoffice 和 php 包装类要容易得多,例如 https://github.com/ncjoes/办公室转换器。
当然,您必须安装 libreoffice 并且您必须完全控制您的网络服务器。
it is much easier to use headless libreoffice and a php wrapper class like https://github.com/ncjoes/office-converter.
of course you have to install libreoffice and you must have full control of your webserver.