word2pdf

在网上现在能查到的,并且能通过php调用相关接口,将word转换成pdf文件的有openoffice,访问网址为http://www.openoffice.org/download/index.html

它提供了各种平台的转换软件。

自己下载后,安装使用。

在windows环境下:

我们下载安装后,首先要启动openoffice 对应的wordpdf的服务
比如我安装的路径是:C:\Program Files\OpenOffice 4\program 我们在cmd命令下进入该路径,运行下面命令 启动wordpdf对应接口。
C:\Program Files\OpenOffice 4\program>soffice -headless -accept='socket,host=127.0.0.1,port=8100;urp;' -nofirststartwizard
然后我们再配置php的调用openoffice word生成pdf的配置, 打开php.ini 然后配置下面的内容
extension=php_com_dotnet.dll
你看看你那里有没有上面的配置,如果没有则加上对应的配置,然后查看一下php扩展文件夹里面有没有php_com_dotnet.dll
一般都在ext文件夹下,如果文件夹下没有则要更换php或者下载对应版本的php_com_dotnet.dll,这个是php用来调用openoffice的扩展文件。
 
再次,我们就要用php代码来实现wordpdf了,看下面的代码,来自网上,部分修改。
<?php
set_time_limit(0);  //设置过期时间
$home = str_replace("\\","/",getcwd());
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 = $home."/word.docx";//输入的 word文件地址,
$output_file = $home. '/pdf.pdf';  //输出的pdf 文件地址,
$doc_file = "file:///" . $doc_file;  
$output_file = "file:///" . $output_file;  
word2pdf($doc_file,$output_file);  
?>
然后运行php文件,上面的word文件就会自动转为pdf文件输出 测试一下吧。
posted @ 2015-01-05 14:36  FookLook  阅读(329)  评论(0编辑  收藏  举报