PHP網(wǎng)站開(kāi)發(fā)之將文檔轉(zhuǎn)換成pdf文件(php動(dòng)態(tài)網(wǎng)站開(kāi)發(fā)的課后答案)

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

本文介紹的是在Windows系統(tǒng)環(huán)境下進(jìn)行的PHP將text、excl、word等等文檔格式轉(zhuǎn)換成pdf文件格式
第一步:檢查是當(dāng)時(shí)的php是否開(kāi)啟對(duì)dcom擴(kuò)展
打開(kāi)php.ini,搜索php_com_dotnet和php_com_dotnet:
extension=php_com_dotnet.dll   //把前面的分號(hào)去掉
com.allow_dcom = true  //改為true
然后輸出下phpinfo()
看看有沒(méi)有com_dotnet
COM support enabled
DCOM support enabled
.Net support       enabled
表示COM組件開(kāi)啟成功
所需要的工具openoffice安裝openoffice軟件
在openoffice安裝成功之后,需要在安裝目錄下porgram文件下打開(kāi)cmd命令行輸入
cd  /d  d:\openoffice\program
具體看你所安裝的具體文件位置
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
配置openoffice服務(wù)啟動(dòng)和激活權(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文件頁(yè)數(shù)的函數(shù)獲取,文件應(yīng)當(dāng)對(duì)當(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"); // 打開(kāi)文件
          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; // 返回頁(yè)數(shù)
          }
      }

      /**
       * office文件轉(zhuǎn)換pdf格式
       * @param  string $input  需要轉(zhuǎn)換的文件
       * @param  string $output 轉(zhuǎn)換后的pdf文件
       * @return return string 頁(yè)數(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;
          // 測(cè)試文檔$input='file:///C:/wamp/www/br/Uploads/Temp/files/20181023/5bcf3e022d9ff.txt';
           // 測(cè)試文檔$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);
   }

基本上完成,開(kāi)發(fā)中遇到的問(wèn)題主要有
Openoffice 服務(wù)沒(méi)有開(kāi)啟;
文件沒(méi)有找到路徑問(wèn)題,要絕對(duì)路徑
COM組件沒(méi)有開(kāi)啟。注意開(kāi)啟在window系統(tǒng)下Openoffice開(kāi)啟后會(huì)很占內(nèi)存,服務(wù)器配置要高一點(diǎn)不然就卡死
 

以上就是關(guān)于PHP網(wǎng)站開(kāi)發(fā)之將文檔轉(zhuǎn)換成pdf文件(php動(dòng)態(tài)網(wǎng)站開(kāi)發(fā)的課后答案),希望對(duì)你有幫助,更多內(nèi)容關(guān)注創(chuàng)新互聯(lián)。

網(wǎng)站題目:PHP網(wǎng)站開(kāi)發(fā)之將文檔轉(zhuǎn)換成pdf文件(php動(dòng)態(tài)網(wǎng)站開(kāi)發(fā)的課后答案)
路徑分享:http://www.muchs.cn/news31/309731.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、關(guān)鍵詞優(yōu)化商城網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、Google

廣告

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

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司