PHP网站开发之将文档转换成pdf文件

发布时间:2025-11-07 点击:4
本文介绍的是在windows系统环境下进行的php将text、excl、word等等文档格式转换成pdf文件格式
第一步:检查是当时的php是否开启对dcom扩展
打开php.ini,搜索php_com_dotnet和php_com_dotnet:
extension=php_com_dotnet.dll //把前面的分号去掉
com.allow_dcom = true //改为true
然后输出下phpinfo()
看看有没有com_dotnet
com support enabled
dcom support enabled
.net support enabled
表示com组件开启成功
所需要的工具openoffice安装openoffice软件
在openoffice安装成功之后,需要在安装目录下porgram文件下打开cmd命令行输入
cd /d d:\openoffice\program
具体看你所安装的具体文件位置
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
配置openoffice服务启动和激活权限具体可以百度 这里不做介绍
下面就是代码介绍
<?php
class openoffice{
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 '参数错误';
}
$input = "file:///" . str_replace("\\", "/", $input);
$output = "file:///" . str_replace("\\", "/", $output);
//dump($input);
//dump($output);exit;
// 测试文档$input='file:///c:/wamp/www/br/uploads/temp/files/20181023/5bcf3e022d9ff.txt';
// 测试文档$output='file:///c:/wamp/www/br/uploads/temp/files/20181023/5bcf3e022d9ff.pdf';
return $this->transform($input, $output);
}
}
调用
function topdf(){
import('org.util.openoffice');
$file_url =$res['file_url']; //原文件
$file_dir = 'e:/wamp/www'.; //下载文件存放目录
$file_info=pathinfo($file_url);
$doc_file = $file_dir . $file_url;
$pdf_file=$file_dir.$file_info['dirname'].'/'.$file_info['filename'].'.pdf';//转换后的pdf名
//dump($pdf_file);
//dump($file_info);exit;
$open=new \openoffice();
// $open->run($doc_file,$pdf_file);
$res=$open->run($doc_file,$pdf_file);
dump($res);
}
基本上完成,开发中遇到的问题主要有
openoffice 服务没有开启;
文件没有找到路径问题,要绝对路径
com组件没有开启。注意开启在window系统下openoffice开启后会很占内存,服务器配置要高一点不然就卡死


域名备案和icp备案区别在哪?
简约风格外贸网站设计有哪些优势
5种方法解决你的下一个网站设计
网站SEO优化的核心:围绕“人”去做
做网站一定要学代码吗?不会代码可以学吗
网站SEO整体的优化内容怎么做?
用php开发网站一下,pHp为啥这么炙手可热,万人追捧吧!(组图)php开发网站
房山医院网站开发_网站建设_量身设计