终极精品

标题: Flash Paper 仿百度文库的功能实现 [打印本页]

作者: zhoji    时间: 2020-4-13 22:40
标题: Flash Paper 仿百度文库的功能实现
具体是哪位大神发给我的,我就忘了,拿出来分享下吧
  1. <?php
  2. /*
  3.     Description: FlashPaper文档转换,转换可打印的文档为FLASH格式并保存转换的相关信息到log_swfread表

  4.     * 使用FlashPaper服务需求
  5.         1. 运行在Windows(xp/2003)服务器下
  6.         2. 能调用系统命令exec
  7.         3. 调用的文件目录必须可读可写
  8.         4. 需要转换的文件类型,服务器必须安装相应的转换软件pdf安装 pdf 9.0, office安装2007,且必须是正版
  9.         5. 服务器需要支持虚拟打印机功能
  10.         6. 服务器的内容要足够大,最少4GB
  11.         7. 在系统管理-站点管理-设置网站URL和访问URL一致

  12.     * 测试FlashPaer
  13.         1.运行初始化中 初始化.bat文件 无任何错误即可(xp/2003)
  14.         2.查看打印机文档中是否有打印机FlashPaer
  15.         3.运行FlashPrinter.exe直接讲要转换的文件拖到此,如果能转换则软件安装无问题

  16.     * API调用方法
  17.         1. 同步调用 -支持多文件同时转换,不支持执行时间检测和转换状态检测
  18.             $flashPaper = A('Api.FlashPaper');
  19.             $flashPaper->convertFile($uploadlist);
  20.         2. 异步调用 -多文件上传需调用多次,支持执行时间检测和转换状态更新
  21.             $url = $this->config['site_url'].'Api/FlashPaper/convertFile';
  22.             foreach($uploadList as $data){
  23.                 post($url,$data);
  24.             }

  25.     * 注意
  26.        1. 多文件同时转换
  27.           - FlashPaper产生多个独立的进程,转换完毕后自动关闭经常和被调用的程序(尚未支持)
  28.        2. 使用pptx/docx/xlsx转换速度快于2003格式

  29.     * 保持转换信息
  30.         - 系统默认保存转换的文件名称,文件大小,文件类型,转换后保持地址,转换时间,转换运行时间和转换失败的错误信息
  31.         - 默认保存的数据库表是log_swfread 保存的方式是execSaveInfo

  32. */
  33. class FlashPaperAction extends Action
  34. {
  35.     public $flashpaper_url = '/Public/FlashPaper/FlashPrinter.exe'; // 应用程序路径
  36.     public $allow_file_ext = 'doc|docx|ppt|pptx|xls|xlsx|pdf|jpg|bmp|gif|png|txt'; // 允许转换的文件格式(可打印的文件)
  37.     public $swf_savepath = '/Data/swfscorce/'; // SWF保存虚拟路径
  38.     public $time_limit = 1000; // 程序最大运行时间

  39.     private $socket = false; // 是否socket请求

  40.     private $scorce_file_path; // 上传文件物理路径
  41.     private $scorce_save_path; // 上传文件的保存路径
  42.     private $score_file_name; // 上传文件保存名称
  43.     private $scorce_file_ext; // 上传文件扩展名
  44.     private $scorce_file_size; // 文件大小
  45.     private $exec_error; // 转换文件错误提示
  46.     private $exec_start_time; // 转换开始时间
  47.     private $exec_end_time; // 转换结束时间
  48.     private $scorce_attch_id; // 附件信息ID号
  49.     private $swf_file_path = ''; // 转换swf文件保存物理路径


  50.     public function test(){
  51.         // 测试各种格式的文件转换
  52.         // 支持的文件: txt pdf ppt pptx jpg gif png bmp xls
  53.         // 正在测试的文件: docx,xlsx,rtf pps pot doc
  54.         $command = 'D:/yjoa/Public/FlashPaper/FlashPrinter.exe -o D:/yjoa/Data/swfScorce/201110/1.docx.swf D:/yjoa/Data/flashpaper/201110/1.xlsx';
  55.         $command = str_replace("/","\\",$command);

  56.         $exec_start_time = $this->exectime();

  57.         exec($command, $return_array);
  58.         $exec_end_time = $this->exectime();

  59.         echo 'Execue Time:'.round($exec_end_time - $exec_start_time, 6).'<br/>';
  60.         echo $command;
  61.     }

  62.     // 转换DEMO页面
  63.     public function _empty()
  64.     {
  65.        $fconfig = F('Config');

  66.        $upload_allow_ext = '';
  67.        $upload_allow_desc = '';
  68.        $upload_allow_extsarr = explode(',', $fconfig['upload_allow_exts']);

  69.        foreach ($upload_allow_extsarr as $v)
  70.        {
  71.            $upload_allow_ext = $upload_allow_ext. '*'.$v.';';
  72.            $upload_allow_desc = $upload_allow_desc. '.'.$v.',';
  73.        }

  74.        $upload_allow_ext = substr($upload_allow_ext, 0, -1);
  75.        $upload_allow_desc = substr($upload_allow_desc, 0, -1);

  76.        $this->assign('upload_allow_desc', $upload_allow_desc);
  77.        $this->assign('upload_allow_ext', $upload_allow_ext);
  78.        $this->display('read');
  79.     }

  80.     /* 初始化 */
  81.     public function _initialize()
  82.     {
  83.         ignore_user_abort(true);
  84.         set_time_limit($this->time_limit);

  85.         // FLASHPAPER转换程序物理地址
  86.         $this->flashpaper_url = $_SERVER['DOCUMENT_ROOT'].$this->flashpaper_url;

  87.         // 自动创建转换保存目录
  88.         @mkdir('.'.$this->swf_savepath);
  89.         @mkdir('.'.$this->swf_savepath.date('Ym'));

  90.         // SWF文件保存虚拟路径
  91.         $this->swf_file_path = $this->swf_savepath.date('Ym');

  92.         // SWF文件保存物理路径
  93.         $this->swf_savepath = $_SERVER['DOCUMENT_ROOT'].$this->swf_savepath.date('Ym').'/';
  94.     }

  95.     /* 调用FlashPaper,$uploadlist是ThinkPHP的UPLOAD上传的数组形式的文件数组信息 */
  96.     public function convertFile($uploadList)
  97.     {

  98.         if (!$uploadList){
  99.             $uploadList = array( 0 => $_POST);
  100.             $this->socket = true;
  101.         }

  102.         if(is_array($uploadList))
  103.         {
  104.             foreach ($uploadList as $v)
  105.             {
  106.                 $this->scorce_file_path = $_SERVER['DOCUMENT_ROOT'].substr($v['savepath'],1).$v['savename'];
  107.                 $this->score_file_name  = strrpos($v['savename'], '.') ? substr($v['savename'],0,strrpos($v['savename'], '.')) : $v['savename'];
  108.                 $this->scorce_file_ext  = $v['extension'];
  109.                 $this->scorce_file_size = $v['size'];
  110.                 $this->scorce_attch_id  = $v['attid'] ? $v['attid'] : 0;
  111.                 $this->scorce_save_path = substr($v['savepath'],1).$v['savename'];
  112.                 $this->execFile();
  113.             }
  114.         }

  115.     }

  116.     // 检测是否满足条件转换
  117.     private function check()
  118.     {
  119.         $err = 0;
  120.         $this->exec_error = '';
  121.         $err_tip = array(
  122.             1 => 'FlashPaper只能运行与Windows环境下',
  123.             2 => 'FlashPrinter.exe转换文件不存在',
  124.             3 => '没有exec函数执行权限',
  125.             4 => 'FlashPrinter('.$this->flashpaper_url.')目录不可读',
  126.             5 => '上传文件('.$this->scorce_file_path.')不存在',
  127.             6 => '保存('.$this->swf_savepath.')目录不存在或不可写',
  128.             7 => '不支持该文件格式('.$this->scorce_file_ext.')的转换',
  129.         );

  130.         // 是否WINDOWS环境
  131.         if(!IS_WIN){
  132.             $err = 1;
  133.         }
  134.         // 检测FlashPaper是否存在
  135.         else if (!file_exists($this->flashpaper_url)){
  136.             $err = 2;
  137.         }
  138.         // 检测exec是否可以执行
  139.         else if (!function_exists('exec')){
  140.             $err = 3;
  141.         }
  142.         // FlashPaper是否可读
  143.         else if (!is_readable($this->flashpaper_url)){
  144.             $err = 4;
  145.         }
  146.         // 源文件是否可读
  147.         else if (!is_readable($this->scorce_file_path)){
  148.             $err = 5;
  149.         }
  150.         // 保存目录是否可写
  151.         else if (is_dir($this->swf_file_path)){
  152.             $err = 6;
  153.         }
  154.         // 检测文件格式
  155.         else if (!in_array($this->scorce_file_ext, explode('|', $this->allow_file_ext))){
  156.             $err = 7;
  157.         }

  158.         if ($err){
  159.             $this->exec_error = $err_tip[$err];
  160.             return false;
  161.         }

  162.         return true;

  163.     }

  164.     // 转换上传文件
  165.     private  function execFile()
  166.     {
  167.         if ($this->check())
  168.         {
  169.             $swf_path = $this->swf_savepath.$this->score_file_name.'.swf ';

  170.             // SWF文件相对路径
  171.             $this->swf_file_path = $this->swf_file_path.'/'.$this->score_file_name.'.swf';

  172.             // 转换SWF命令
  173.             $command = $this->flashpaper_url.' -o '.$swf_path.$this->scorce_file_path;
  174.             $command = str_replace("/","\\",$command);

  175.             // 纪录转换时间
  176.             $this->exec_start_time = $this->exectime();

  177.             // 调用FlashPaper执行转换
  178.             exec($command);

  179.             $this->exec_end_time = $this->exectime();

  180.         }

  181.         // 保存执行信息
  182.         $this->execSaveInfo();

  183.     }

  184.     // 记录转换运行数据
  185.     private function execSaveInfo()
  186.     {
  187.         $state = 0;  // 转换状态,默认未转换
  188.         $time = time();
  189.         $runtime = round($this->exec_end_time - $this->exec_start_time, 6);
  190.         $scorcepath = $this->scorce_file_path;

  191.         $swfread = M('swfread');

  192.         if ($this->exec_error == '')
  193.         {
  194.             $sql = "INSERT INTO `log_swfread` (attid,filename,filesize,filetype,`time`,runtime,`state`,filepath,scorcepath)
  195.                 VALUES
  196.                ('{$this->scorce_attch_id}',
  197.                '".$this->score_file_name.'.'.$this->scorce_file_ext."',
  198.                '{$this->scorce_file_size}',
  199.                '{$this->scorce_file_ext}',
  200.                '{$time}',
  201.                '{$runtime}',
  202.                '{$state}',
  203.                '{$this->swf_file_path}',
  204.                '{$this->scorce_save_path}'
  205.                );";
  206.         }
  207.         else
  208.         {
  209.             // 转换失败
  210.             $sql = "INSERT INTO `log_swfread` (attid,filename,filesize,filetype,`time`,runtime,`state`,filepath,scorcepath,`error`)
  211.                 VALUES
  212.                ('{$this->scorce_attch_id}',
  213.                '".$this->score_file_name.'.'.$this->scorce_file_ext."',
  214.                '{$this->scorce_file_size}',
  215.                '{$this->scorce_file_ext}',
  216.                '{$time}',
  217.                '{$runtime}',
  218.                '0',
  219.                '{$this->swf_file_path}',
  220.                '{$this->scorce_save_path}',
  221.                '{$this->exec_error}'
  222.                );";
  223.         }

  224.         $swfread->execute($sql);
  225.         $swfid = $swfread->query("SELECT MAX(swfid) as id FROM `log_swfread`");

  226.         // socket模式:请求监听生成状态和执行时间
  227.         if ($this->socket && $this->exec_error == ''){
  228.             $i = 1;
  229.             while(true){
  230.                 if (file_exists('.'.$this->swf_file_path)){
  231.                     $this->exec_end_time = $this->exectime();
  232.                     $runtime = round($this->exec_end_time - $this->exec_start_time, 6);
  233.                     $sql = "UPDATE log_swfread SET `state` = 1 , runtime = '{$runtime}' WHERE swfid = '{$swfid[0]['id']}'";
  234.                     $swfread->query($sql);
  235.                     break;
  236.                 }
  237.                 if (++$i > $this->time_limit-100) break;
  238.                 sleep(1);
  239.             }
  240.         }
  241.     }

  242.     // 执行时间
  243.     private function exectime()
  244.     {
  245.         $time = explode(" ", microtime());
  246.         $usec = (double)$time[0];
  247.         $sec = (double)$time[1];
  248.         return $sec + $usec;
  249.     }
  250. }
  251. ?>
复制代码







欢迎光临 终极精品 (http://www.chnspy.com/) Powered by Discuz! X3.2