近期做一个项目,须要将用户上传的word,excel文档转成PDF文档保存并打印。在网上找了非常多资料。并不全面,所以自己写了一份比較全面的教程来分享。

以下是操作步骤:

1、        安装免费的openOffice软件。请至openoffice.org下载最新版本号。

2、        JDK支持,请自行搜索下载最新版本号JDK。

3、        安装完openOffice后,在開始--执行中输入Dcomcnfg打开组件服务。在组件服务—计算机—我的电脑—DCOMP配置中,选择

在这两项上分别点击右键属性。打开属性面板例如以下图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvanpoMjAwNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

选择安全选项卡,分别在启动和激活权限和訪问权限两项上点自己定义。加入Everyone的权限。

选择标识选项卡。选择交互式用户。

4、        安装完openOffice后。请先打开一次确认能够正常执行软件,然后退出后用命令行执行下面命令。

先到安装文件夹下,比如:C:\Program Files\OpenOffice 4\program\

运行命令:

soffice -headless-accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

成功后即在后台执行了该软件。

5、        假设是php5.4.5曾经版本号,须要在php.ini里把com.allow_dcom = true打开。即去掉前面的分号。假设是以后版本号,须要在php.ini 里添加一行扩展extension=php_com_dotnet.dll。然后检查php的ext文件夹中是否存在该dll文件。假设没有请自行下载相应版本号的dll。然后重新启动apache或IISserver。

6、        代码实现

/**
* office文档转换为PDF类
* @author jinzhonghao <954299193@qq.com> created 2015-04-23
*/ class office2pdf
{
private $osm; public function __construct()
{
$this->osm = new COM("com.sun.star.ServiceManager")or die ("Please be sure that OpenOffice.org is installed.n");
} public function MakePropertyValue($name,$value)
{
$oStruct = $this->osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue");
$oStruct->Name = $name;
$oStruct->Value = $value;
return $oStruct;
} public 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);
} public function run($input,$output)
{
$input = "file:///" . str_replace("\\","/",$input);
$output = "file:///" . str_replace("\\","/",$output);
return $this->transform($input, $output);
} /**
* 获取PDF文件页数的函数获取
* 文件应当对当前用户可读(linux下)
* @param [string] $path [文件路径]
* @return int
*/
public 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;
}
} }

最新文章

  1. 平衡二叉查找树(AVL)的理解与实现
  2. PHP中strtotime函数使用方法分享
  3. Form - 遍历行
  4. the third assignment of software testing
  5. react + iscroll5
  6. Show All Running Processes in Linux
  7. CSS权威指南学习笔记系列(1)CSS和文档
  8. sort,uniq命令
  9. 关于uitableView的Group模式滑动偏移问题
  10. [Ext JS 4]性能优化
  11. Java NIO的探究
  12. squid调整
  13. python3之requests
  14. Flask源码阅读-第四篇(flask\app.py)
  15. Ubuntu 14.10 下SSH配置
  16. 让HTML5来为你定位(转)
  17. python应用之爬虫实战1 爬虫基本原理
  18. 洛谷P1972 HH的项链
  19. 【异常记录(七)】MVC:从客户端中检测到有潜在危险的 Request.Form 值 的解决方法 [转]
  20. HDU - 6215 2017 ACM/ICPC Asia Regional Qingdao Online J - Brute Force Sorting

热门文章

  1. 面向对象——property
  2. Leetcode 477.汉明距离总和
  3. API生命周期第三阶段:API实施模式,以及结合swagger和项目现状的最佳模式
  4. 九度oj 题目1080:进制转换
  5. 九度oj 1005
  6. 硅谷和国内的 iOS 开发到底有何不同?
  7. ie,360浏览器出现无法打开网页(包括本地html)的解决方法
  8. HDU——2054A==B?
  9. 浅谈android反调试之 转发端口
  10. 使用gcc的-finstrument-functions选项进行函数跟踪【转】