PHP调用OpenOffice实现word转PDF的方法

2025-05-29 0 76

最近一直在研究PHP word文档转PDF,也在网上搜索了很多类似的资料,大多数都是通过OpenOffice进行转换的。

核心的代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24
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){

$osm = new COM("com.sun.star.ServiceManager") or die ("Please be sure that OpenOffice.org is installed.n");

$args = array(MakePropertyValue("Hidden",true,$osm));

$oDesktop = $osm->createInstance("com.sun.star.frame.Desktop");

$oWriterDoc = $oDesktop->loadComponentFromURL($doc_url,"_blank", 0, $args);

$export_args = array(MakePropertyValue("FilterName","writer_pdf_Export",$osm));

$oWriterDoc->storeToURL($output_url,$export_args);

$oWriterDoc->close(true);

}

$doc_file=dirname(__FILE__)."/11.doc"; //源文件,DOC或者WPS都可以

$output_file=dirname(__FILE__)."/11.pdf"; //欲转PDF的文件名

$doc_file = "file:///" . $doc_file;

$output_file = "file:///" . $output_file;

$document->word2pdf($doc_file,$output_file);

用上述发现代码一直在报错

( ! ) Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> [automation bridge] <br/><b>Description:</b> com.sun.star.task.ErrorCodeIOException: ' in I:\\phpStudy\\WWW\\DocPreview\\test2.php on line 27

( ! ) com_exception: <b>Source:</b> [automation bridge] <br/><b>Description:</b> com.sun.star.task.ErrorCodeIOException: in I:\\phpStudy\\WWW\\DocPreview\\test2.php on line 27

最后发现原来是转出路径的问题:通过调试得出上述代码的转出路径$output_file 是file:///I:\\phpStudy\\WWW\\DocPreview\\sdds.pdf。

然而storeToURL这个方法里面需要的路径是这样的:file:///I:/phpStudy/WWW/DocPreview/sdds.pdf。

因此只需要将$output_file的"\\"替换为“/”

?

1

2

3

4

5

6
$doc_file=dirname(__FILE__)."/11.doc"; //源文件,DOC或者WPS都可以

$output_file=dirname(__FILE__)."/11.pdf"; //欲转PDF的文件名

$output_file=str_replace("\\\\","/",$output_file);

$doc_file = "file:///" . $doc_file;

$output_file = "file:///" . $output_file;

$document->word2pdf($doc_file,$output_file);

以上这篇PHP调用OpenOffice实现word转PDF的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

原文链接:http://blog.csdn.net/falys/article/details/51869373

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 PHP调用OpenOffice实现word转PDF的方法 https://www.kuaiidc.com/93376.html

相关文章

发表评论
暂无评论