PHP網(wǎng)站開發(fā)之將文檔轉(zhuǎn)換成pdf文件

2023-02-26    分類: 網(wǎng)站建設(shè)

本文介紹的是在Windows系統(tǒng)環(huán)境下進(jìn)行的PHP將text、excl、word等等文檔格式轉(zhuǎn)換成pdf文件格式
第一步:檢查是當(dāng)時(shí)的php是否開啟對dcom擴(kuò)展
打開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服務(wù)啟動和激活權(quán)限具體可以百度 這里不做介紹
下面就是代碼介紹
<?php
class OpenOffice{
private $osm;
// 構(gòu)造函數(shù),啟用OpenOffice的COM組件
public function __construct(){
ini_set('magic_quotes_runtime', 0); // 設(shè)置運(yùn)行時(shí)間
$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文件頁數(shù)的函數(shù)獲取,文件應(yīng)當(dāng)對當(dāng)前用戶可讀(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; // 返回頁數(shù)
}
}
/**
* office文件轉(zhuǎn)換pdf格式
* @param string $input 需要轉(zhuǎn)換的文件
* @param string $output 轉(zhuǎn)換后的pdf文件
* @return return string 頁數(shù)
*/
public function run($input = '', $output = ''){
if(empty($input) || empty($output)){
return '參數(shù)錯(cuò)誤';
}
$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);
}
}
調(diào)用
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';//轉(zhuǎn)換后的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);
}
基本上完成,開發(fā)中遇到的問題主要有
Openoffice 服務(wù)沒有開啟;
文件沒有找到路徑問題,要絕對路徑
COM組件沒有開啟。注意開啟在window系統(tǒng)下Openoffice開啟后會很占內(nèi)存,服務(wù)器配置要高一點(diǎn)不然就卡死

新聞標(biāo)題:PHP網(wǎng)站開發(fā)之將文檔轉(zhuǎn)換成pdf文件
轉(zhuǎn)載源于:http://www.bm7419.com/news28/239778.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、外貿(mào)建站、網(wǎng)站策劃網(wǎng)站收錄、網(wǎng)站排名、網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)