PHP网站开发之将文档转换成pdf文件(php动态网站开发的课后答案)

时间: 2022-08-02 08:23:36 浏览次数:231
本文介绍的是在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开启后会很占内存,服务器配置要高一点不然就卡死
 
以上就是关于PHP网站开发之将文档转换成pdf文件(php动态网站开发的课后答案),希望对你有帮助,更多内容关注蓝港网络

  非常感谢您读完蓝港网络的这篇文章:"PHP网站开发之将文档转换成pdf文件(php动态网站开发的课后答案)",仅为提供更多信息供用户参考使用或为学习交流的方便。我们公司提供:网站建设、网站制作、官网建设、SEO优化、小程序制作等服务,欢迎联系我们提供您的需求。

标签:

Copyright © 常州蓝港网络科技有限公司 苏ICP备2022017479号-1