关于PHP调用com组件实现word转pdf的问题

我按照网上的教程操作了一遍,一直报如下错误:

Fatal error: Call to a member function storeToURL() on a non-object in D:\Xampp\htdocs\ctmgr\protected\modules\proj\views\project\wordtopdf.php on line 47

找了半天也不知道为什么会出现错误,我的代码如下:
<?php

class RunTime //页面执行时间类

{

private $starttime;//页面开始执行时间

private $stoptime;//页面结束执行时间

private $spendtime;//页面执行花费时间

function getmicrotime()//获取返回当前微秒数的浮点数

{

list($usec,$sec)=explode(" ",microtime());

return ((float)$usec + (float)$sec);

}

function start()//页面开始执行函数,返回开始页面执行的时间

{

$this->starttime=$this->getmicrotime();

}

function end()//显示页面执行的时间

{

$this->stoptime=$this->getmicrotime();

$this->spendtime=$this->stoptime-$this->starttime;

//return round($this->spendtime,10);

}

function display()

{

//$this->end();

echo "

运行时间:".round($this->spendtime,10)."秒

";

}

}

/*调用方法 */

$timer=new Runtime();

$timer->start();

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);

}

$output_dir = "D:/";

$doc_file = "D:/RA.doc";

$pdf_file = "RA.pdf";

$output_file = $output_dir . $pdf_file;

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

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

word2pdf($doc_file,$output_file);

$timer->end();

$timer->display();

?>

找到对应出错的代码行数,然后检查对应的对象是否创建成功。应该是对象创建失败,导致调用方法出错

https://blog.csdn.net/falys/article/details/51869373,可以看下这个地址很详细,我还犯了一个坑,就是我的文档是只读模式,所以就一直失败。

九云图接口可以把 OFFICE / PDF / CAD 等文档转换成 PDF、HTML5(SVG)、长图片 等格式。在移动端缺少字库的情况下,也能保持字体和版式完全不变,支持添加水印、高清平滑缩放。转换生成的 HTML 不含任何脚本和外链。调用地址:http(s)://api.9yuntu.cn/execute/Convert
请求方式:GET
返回类型:JSON
API 调用:API 简单身份认证调用方法(APPCODE)展开API 签名认证调用方法(AppKey & AppSecret)
请求参数(Headers)
请求参数(Query)
docURLSTRING必选源文档URL,需进行URL编码。
outputTypeSTRING可选转换输出的格式,取值包括: "webview"(默认)- 返回长度为1的 URL 数组。数组成员指向一个可在浏览器中展现文档的URL,不支持下载; "html" - 返回长度为1的 URL 数组。数组成员指向一个包括所有页面内容的完整 HTML,不含任何脚本和外链,图片均以 svg 或 base64 形式内嵌,支持下载; "htmls" - 返回 URL 数组。文档的每个页面转换成一个独立的 HTML,不含任何脚本和外链,图片均以 svg 或 base64 形式内嵌,支持下载; "pdf" - 返回长度为1的 URL 数组。数组成员指向转换成的PDF文件,支持下载; "longimage" - 返回长度为1的 URL 数组。数组成员指向转换成的长图片,支持下载; "images" - 返回 URL 数组。文档的每个页面转换成一个单独的图片,支持下载; "svgs" - 返回 URL 数组。文档的每个页面转换成一个单独的 SVG 文件,支持下载。
titleSTRING可选在浏览器标题栏上显示的标题,默认是文件名称。需进行URI编码。
watermarkSTRING可选文档水印。可以是一段文字,也可以是图片URL。需进行URI编码。
请求参数(Body)
请求示例
curl
Java
C#
PHP
Python
ObjectC
curl -i -k --get --include'https://api.9yuntu.cn/execute/Convert?docURL=http%253A%252F%252Foss.9yuntu.cn%252Fresources%252Fapi%252F%25E4%25B9%259D%25E4%25BA%2591%25E5%259B%25BEAPI%25E4%25BD%25BF%25E7%2594%25A8%25E8%25AF%25B4%25E6%2598%258E.docx&outputType=webview&title=%25e4%25b9%259d%25e4%25ba%2591%25e5%259b%25be&watermark=watermark'-H'Authorization:APPCODE 你自己的AppCode'
正常返回示例
{"retCode":1,"retMsg":"文档正在处理中,请使用 QueryStatus 轮询转换结果。","docID":"xxxxxxxxxxxxx","outputType":"html","docStatusPage":"http://server.9yuntu.cn/execute/DocStatusPage?token=xxxxxxxxx"}