PHP Windows系统下调用OpenOffice
项目需要把用户上传的word文档转换为pdf文件,方便用户浏览。经过谷歌百度找到PHP可以使用COM组件调用微软的openoffice来实现文档转换
1,安装OpenOffice
- 安装OpenOffice 4.1.3 (zh-CN),可百度直接下载对应操作系统版本
2,设置权限
- cmd 运行Dcomcnfg.exe->组件服务->计算机->我的电脑->DCOM配置->OpenOffice Service Manager
- 鼠标右击->属性,选择安全 ,和标识这2个配置。标识配置=>交互式用户,安全=>自定义,全部添加Everyone权限。
- 点击编辑->添加Everyone权限就行了
- 启动OpenOffice服务命令:
- 打开cmd(建议用管理员权限运行,保证服务正常开启)。 先进入OpenOffice安装目录,例如我安装的:
- cd C:\Program Files (x86)\OpenOffice 4\program
- 启动服务:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
3,配置php.ini
- php.ini 开启 com.allow_dcom = true
- php.ini 添加 extension=php_com_dotnet.dll
- 检查该文件 php/ext/php_com_dotnet.dll
下面奉上我整理的代码,希望能帮到你
<?php
/**
* office文档转换类
* 实现office任何格式文档网页浏览
* author hui
* 1,安装OpenOffice 4.1.3 (zh-CN)
*
* 2,安装 SWFTOOLS http://www.swftools.org/download.html到C盘
* 并把pdf2swf.exe文件移动到C盘根目录
*
* 3,php.ini 开启com.allow_dcom = true
* php.ini 添加extension=php_com_dotnet.dll
* 检查该文件
* php/ext/php_com_dotnet.dll
*/ class Convert{ private $osm; // 构造函数,启用OpenOffice的COM组件
public function __construct(){
ini_set('magic_quotes_runtime', 0); // 设置运行时间
$this->osm = new COM("com.sun.star.ServiceManager") or die("Please be sure that OpenOffice.org is installed.n");
} private function MakePropertyValue($name, $value){
$oStruct = $this->osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue");
$oStruct->Name = $name;
$oStruct->Value = $value;
return $oStruct;
} private function transform($input_url, $output_url){
$args = array($this->MakePropertyValue('Hidden', true));
$oDesktop = $this->osm->createInstance("com.sun.star.frame.Desktop");
$oWriterDoc = $oDesktop->loadComponentFromURL($input_url, '_blank', 0, $args);
$export_args = array($this->MakePropertyValue('FilterName', 'writer_pdf_Export'));
$oWriterDoc->storeToURL($output_url, $export_args);
$oWriterDoc->close(true);
return $this->getPdfPages($output_url);
} /**
* getPdfPages 获取PDF文件页数的函数获取,文件应当对当前用户可读(linux下)
* @param string $path 文件路径
* @return int
*/
private function getPdfPages($path = ''){
if(!file_exists($path)) return 0;
if(!is_readable($path)) return 0;
$fp=@fopen($path, "r"); // 打开文件
if(!$fp){
return 0;
}else{
$max = 0;
while(!feof($fp)){
$line = fgets($fp,255);
if(preg_match('/\/Count [0-9]+/', $line, $matches)){
preg_match('/[0-9]+/', $matches[0], $matches2);
if ($max<$matches2[0]) $max = $matches2[0];
}
}
fclose($fp);
return $max; // 返回页数
}
} /**
* office文件转换pdf格式
* @param string $input 需要转换的文件
* @param string $output 转换后的pdf文件
* @return return string 页数
*/
public function run($input = '', $output = ''){
if(empty($input) || empty($output)){
return ['error' => 1, 'msg' => '参数缺失', 'flag' => 'run'];
}
$input = "file:///" . str_replace("\\", "/", $input);
$output = "file:///" . str_replace("\\", "/", $output);
return $this->transform($input, $output);
} /**
* pdf2swf pdf文件转换swf格式
* @param string $word_file 需要转换的文件路径
* @param string $attach_dir 保存文件地址
* @return array
*/
public function pdf2swf($word_file = '', $attach_dir = ''){
if(empty($word_file) || empty($attach_dir)){
return ['error' => 1, 'msg' => '参数缺失', 'flag' => 'pdf2swf'];
}
$file_name = uniqid();
$pdf_file = "{$attach_dir}{$file_name}.pdf"; // PDF文件绝对路径
$page = $this->run($word_file, $pdf_file); // 文件先转换为PDF格式
if(isset($page) && $page > 0){
$swf_file = "{$attach_dir}{$file_name}.swf"; // 转换后的swf文件
$pd = str_replace("/", "\\", $pdf_file);
$sw = str_replace("/", "\\", $swf_file);
$cmd = Config::get('websetup.swftools') . " -t {$pd} -s flashversion=9 -o {$sw}";
$phpwsh = new COM("Wscript.Shell") or die("Create Wscript.Shell Failed!");
$exec = $phpwsh->exec("cmd.exe /c" . $cmd); // cmd执行pdf2swf转换命令
$stdout = $exec->stdout();
$stdout->readall();
if(is_file($sw)){ // swf文件
if(is_file($pdf_file)){ // 删除pdf文件
unlink($pdf_file);
}
return ['error' => 0, 'page' => $page, 'swf_file' => $file_name];
}else{
return ['error' => 1, 'msg' => 'swf文件不存在', 'flag' => 'pdf2swf'];
}
}else{
return ['error' => 1, 'msg' => '转换pdf失败', 'flag' => 'pdf2swf'];
}
} }
PHP Windows系统下调用OpenOffice的更多相关文章
- Delphi - Windows系统下,Delphi调用API函数和7z.dll动态库,自动把文件压缩成.tar.gz格式的文件
项目背景 应欧美客户需求,需要将文件压缩成.tar.gz格式的文件,并上传给客户端SFTP服务器. 你懂的,7-Zip软件的显著特点是文件越大压缩比越高,在Linux系统上相当于我们Windows系统 ...
- 如何用python在Windows系统下,生成UNIX格式文件
平时测试工作中,少不了制造测试数据.最近一个项目,我就需要制造一批可在UNIX下正确读取的文件.为确保这批文件能从FTP下载成功,开发叮嘱我:“文件中凡是遇到换行,换行符必须是UNIX下的LF,而不是 ...
- Windows系统下Memcached缓存系列二:CouchbaseClient(c#客户端)的详细试用,单例模式
在上一篇文章里面 ( Windows系统下Memcached缓存系列一:Couchbase(服务器端)和CouchbaseClient(c#客户端)的安装教程 ),我们介绍了服务器端的安装和客户端的安 ...
- 吻逗死(windows)系统下自动部署脚本(for java spring*)及linux命令行工具
转载请注明出处:https://www.cnblogs.com/funnyzpc/p/10051647.html (^^)(^^)自動部署腳本原本在上個公司就在使用,由於近期同事需要手動部署一個Spr ...
- windows系统下npm升级的正确姿势以及原理
本文来自网易云社区 作者:陈观喜 网上关于npm升级很多方法多种多样,但是在windows系统下不是每种方法都会正确升级.其中在windows系统下主要的升级方法有以下三种: 首先最暴力的方法删掉no ...
- windows系统下jenkins环境搭建与基本使用
一. windows 系统下搭建jenkins环境 1.1 jenkins环境搭建和构建job流程图 1.2 安装jdk JDK下载地址: http://www.oracle.com/technet ...
- Windows系统下三十款优秀开源软件
Windows系统下三十款优秀开源软件 1.Firefox 官方网站:http://www.getfirefox.com/ 可替换Internet Explorer 功能特点:如果你还没有使用Fire ...
- 在Windows系统下用命令把应用程序添加到系统服务
在Windows系统下用命令把应用程序添加到系统服务,使用SC命令. 加入服务格式如下:sc create ServiceName binPath= 程序路径 start= auto(等号后面的空格是 ...
- windows系统下在dos命令行kill掉被占用的pid (转)
原文出自:http://www.2cto.com/os/201304/203771.html windows系统下在dos命令行kill掉被占用的pid 1.开始-->运行-->c ...
随机推荐
- Java中的 super和this
super关键字 在子类中用于表示父类对象的引用,可以在子类中调用父类中的方法的属性. super语句 --- 子类在继承父类之后,子类的构造方法中会含有一个super语句. 如果没有手动指定supe ...
- PTA 1004 Counting Leaves (30)(30 分)(dfs或者bfs)
1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...
- Linux命令-chmod、chown和chgrp
Linux系统中的每个文件和目录都有访问许可权限,用它来确定谁可以通过何种方式对文件和目录进行访问和操作. 文件或目录的访问权限分为只读,只写和可执行三种.以文件为例,只读权限表示只允许读其内容,而禁 ...
- 读取web外的配置文件
一般web项目配置文件都放在classPath下面,读取的时候: 1 import java.io.InputStream; 2 import java.util.Properties; 3 publ ...
- 【POJ】3616 Milking Time(dp)
Milking Time Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10898 Accepted: 4591 Des ...
- apache commons lang包中的StringUtils
计算一个字符串某个字符的出现次数 a, 使用charAt方法截取之后,循环判断. b, 使用apache commons lang包中的StringUtils: int n = StringUtils ...
- Solr 6.0 学习(五)solr基本查询和高级查询
参考:http://www.cnblogs.com/rainbowzc/p/4354224.html 查询参数 常用: q - 查询字符串,必须的. fl - 指定返回那些字段内容,用逗号或空格分隔多 ...
- mysql注入快速学习基础
前言: sql注入想学好,学通.必须得了解一下基础的SQL 语句.这里我快速理一理 正文: 搭建环境建议下phpsduy快速搭建 select * from kasi select 字段名 from ...
- 【Eclipse】开发专题
Eclipse插件安装 参考以下几个网页内容 不同版本Eclipse对JDK版本要求http://blog.csdn.net/kevin_pso/article/details/54971739 Ec ...
- mac 使用svn记录
checkout project : svn checkout svn://127.0.0.1/repository --username=username --password=password ...