锘?!DOCTYPE html> 状元红顶尖高手坛-免费完整资料
400-800-9385
缃?绔?寤鸿?璧?璁??缁?

PHP缃?绔?寮???涔?灏???妗h浆?㈡??pdf??浠?/h1>
??琛ㄦ?ユ??锛?2019-03-19 09:17:24   浣????ユ?锛??圭淮缃?缁?   娴?瑙?锛?3797   ??绛撅?PHP缃?绔?寮???    

????浠?缁???????indows绯荤????涓?杩?琛???PHP灏?text??excl??word绛?绛???妗f?煎?杞??㈡??pdf??浠舵?煎?
绗??姝ワ?妫??ユ????剁??php???﹀????dcom?╁?
??寮?php.ini锛???绱?hp_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缁?浠跺???????
????瑕???宸ュ??penoffice瀹?瑁?openoffice杞?欢
??penoffice瀹?瑁?????涔???锛???瑕??ㄥ?瑁????涓?porgram??浠朵???寮?cmd?戒护琛?杈???br /> cd  /d  d:\openoffice\program
?蜂???浣???瀹?瑁????蜂???浠朵?缃?br /> soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
??缃?penoffice???″???ㄥ??婵?娲绘?????蜂???互?惧害 杩???涓???浠?缁?
涓??㈠氨??唬??浠?缁?
<?php
class  OpenOffice{
     private $osm;
    
    // ?????芥?帮?????penOffice??COM缁?浠?br />     public function __construct(){
        ini_set('magic_quotes_runtime', 0); // 璁剧疆杩?琛??堕??br />         $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"); // ??寮???浠?br />           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; // 杩???椤垫??br />           }
      }
 
      /**
       * office??浠惰浆??df?煎?
       * @param  string $input  ??瑕?杞??㈢????浠?br />        * @param  string $output 杞??㈠????pdf??浠?br />        * @return return string 椤垫??br />        */
      public function run($input = '', $output = ''){
          if(empty($input) || empty($output)){
               return '???伴??璇?#39;;
          }
          $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);
      }
 
   
    
 }
璋???br />  function topdf(){
   import('org.Util.OpenOffice');
  
   $file_url =$res['file_url'];     //????浠?nbsp;  
   $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缁?浠舵病??寮?????娉ㄦ??寮?????indow绯荤?涓?Openoffice寮?????浼?寰?????瀛?锛????″?ㄩ??缃??楂?涓??逛??跺氨?℃?
 
濡?娌$?规?娉ㄦ??锛???绔???涓烘?圭淮缃?缁?????,杞?浇璇锋敞???ヨ??ttps:/news/4957.html