本文目录一览:
php word转pdf 有什么方法吗
PHP也可以实现导出Word文档为PDF的功能,不过要借助于第三方的类库,今天我们将为大家介绍PHP依靠com.sun.star.ServiceManager来转换Word为PDF文档的相关技巧。
PHP处理Word转PDF的示例代码:
02 set_time_limit(0);
03 function MakePropertyValue($name,$value,$osm){
04 $oStruct=$osm-Bridge_GetStruct("com.sun.star.beans.PropertyValue");
05 $oStruct-Name = $name;
06 $oStruct-Value = $value;
07 return $oStruct;
08 }
09 function word2pdf($doc_url, $output_url){
10 $osm = new COM("com.sun.star.ServiceManager")or die ("请确认OpenOffice.org库是否已经安装./n");
11 $args = array(MakePropertyValue("Hidden",true,$osm));
12 $oDesktop = $osm-createInstance("com.sun.star.frame.Desktop");
13 $oWriterDoc = $oDesktop-loadComponentFromURL($doc_url,"_blank", 0, $args);
14 $export_args = array(MakePropertyValue("FilterName","writer_pdf_Export",$osm));
15 $oWriterDoc-storeToURL($output_url,$export_args);
16 $oWriterDoc-close(true);
17 }
18 $output_dir = "D:/temp/";
19 $doc_file = "D:/temps/test.doc";
20 $pdf_file = "test.pdf";
21 $output_file = $output_dir.$pdf_file;
22 $doc_file = "".$doc_file;
23 $output_file = "".$output_file;
24 word2pdf($doc_file,$output_file);
25 ?
php word 转pdf 有哪些方法
1、 安装免费的openOffice软件,请至openoffice.org下载最新版本。
2、 JDK支持,请自行搜索下载最新版本JDK。
3、安装完openOffice后,在开始--运行中输入Dcomcnfg打开组件服务。在组件服务—计算机—我的电脑—DCOMP配置中,选择
在这两项上分别点击右键属性,打开属性面板如下图:
选择安全选项卡,分别在启动和激活权限和访问权限两项上点自定义,添加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或IIS服务器。
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 = "" . str_replace("//","/",$input);
$output = "" . 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;
}
}
}
php 怎么实现 word,excel 转为 pdf 格式
最简单的办法就是安装word和excel的pdf插件。很多时候安装包已经集成有该插件了,你看一下文件另存为那里是否能选择pdf类型,没有的话就下载并安装插件就行了非常简单。当然按照楼上的用虚拟打印机打印另存为pdf也是可以的。
PHP word转pdf
这个不是说的很清楚了吗
Please be sure that OpenOffice.org is installed
你直接安装上openoffice,启动 openoffice服务