使用 PHP 将文档转换为 PDF +开放办公

发布于 2024-10-11 15:19:12 字数 2021 浏览 2 评论 0原文

我正在尝试遵循使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

穿透光 2024-10-18 15:19:12

您必须安装 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

千秋岁 2024-10-18 15:19:12

请在您的目录中安装 Open Office。 OpenOffice 设置为在该计算机上作为服务运行。

Please install Open Office in your Directory. OpenOffice setup to run as a service on that machine.

白馒头 2024-10-18 15:19:12

使用无头 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文