<?php <br />
/*<br>
Description: FlashPaper文档转换,转换可打印的文档为FLASH格式并保存转换的相关信息到log_swfread表<br>
<br>
* 使用FlashPaper服务需求<br>
1. 运行在Windows(xp/2003)服务器下<br>
2. 能调用系统命令exec<br>
3. 调用的文件目录必须可读可写<br>
4. 需要转换的文件类型,服务器必须安装相应的转换软件pdf安装 pdf 9.0, office安装2007,且必须是正版<br>
5. 服务器需要支持虚拟打印机功能<br>
6. 服务器的内容要足够大,最少4GB<br>
7. 在系统管理-站点管理-设置网站URL和访问URL一致<br>
<br>
* 测试FlashPaer<br>
1.运行初始化中 初始化.bat文件 无任何错误即可(xp/2003)<br>
2.查看打印机文档中是否有打印机FlashPaer<br>
3.运行FlashPrinter.exe直接讲要转换的文件拖到此,如果能转换则软件安装无问题<br>
<br>
* API调用方法<br>
1. 同步调用 -支持多文件同时转换,不支持执行时间检测和转换状态检测<br>
$flashPaper = A('Api.FlashPaper');<br>
$flashPaper->convertFile($uploadlist);<br>
2. 异步调用 -多文件上传需调用多次,支持执行时间检测和转换状态更新<br>
$url = $this->config['site_url'].'Api/FlashPaper/convertFile';<br>
foreach($uploadList as $data){<br>
post($url,$data);<br>
}<br>
<br>
* 注意<br>
1. 多文件同时转换<br>
- FlashPaper产生多个独立的进程,转换完毕后自动关闭经常和被调用的程序(尚未支持)<br>
2. 使用pptx/docx/xlsx转换速度快于2003格式<br>
<br>
* 保持转换信息<br>
- 系统默认保存转换的文件名称,文件大小,文件类型,转换后保持地址,转换时间,转换运行时间和转换失败的错误信息<br>
- 默认保存的数据库表是log_swfread 保存的方式是execSaveInfo<br>
<br>
*/<br>
class FlashPaperAction extends Action<br>
{<br>
public $flashpaper_url = '/Public/FlashPaper/FlashPrinter.exe'; // 应用程序路径<br>
public $allow_file_ext = 'doc|docx|ppt|pptx|xls|xlsx|pdf|jpg|bmp|gif|png|txt'; // 允许转换的文件格式(可打印的文件)<br>
public $swf_savepath = '/Data/swfscorce/'; // SWF保存虚拟路径<br>
public $time_limit = 1000; // 程序最大运行时间<br>
<br>
private $socket = false; // 是否socket请求<br>
<br>
private $scorce_file_path; // 上传文件物理路径<br>
private $scorce_save_path; // 上传文件的保存路径<br>
private $score_file_name; // 上传文件保存名称<br>
private $scorce_file_ext; // 上传文件扩展名<br>
private $scorce_file_size; // 文件大小<br>
private $exec_error; // 转换文件错误提示<br>
private $exec_start_time; // 转换开始时间<br>
private $exec_end_time; // 转换结束时间<br>
private $scorce_attch_id; // 附件信息ID号<br>
private $swf_file_path = ''; // 转换swf文件保存物理路径<br>
<br>
<br>
public function test(){<br>
// 测试各种格式的文件转换<br>
// 支持的文件: txt pdf ppt pptx jpg gif png bmp xls<br>
// 正在测试的文件: docx,xlsx,rtf pps pot doc<br>
$command = 'D:/yjoa/Public/FlashPaper/FlashPrinter.exe -o D:/yjoa/Data/swfScorce/201110/1.docx.swf D:/yjoa/Data/flashpaper/201110/1.xlsx';<br>
$command = str_replace("/","\\",$command);<br>
<br>
$exec_start_time = $this->exectime();<br>
<br>
exec($command, $return_array);<br>
$exec_end_time = $this->exectime();<br>
<br>
echo 'Execue Time:'.round($exec_end_time - $exec_start_time, 6).'<br>';<br>
echo $command;<br>
}<br>
<br>
// 转换DEMO页面<br>
public function _empty()<br>
{<br>
$fconfig = F('Config');<br>
<br>
$upload_allow_ext = '';<br>
$upload_allow_desc = '';<br>
$upload_allow_extsarr = explode(',', $fconfig['upload_allow_exts']);<br>
<br>
foreach ($upload_allow_extsarr as $v)<br>
{<br>
$upload_allow_ext = $upload_allow_ext. '*'.$v.';';<br>
$upload_allow_desc = $upload_allow_desc. '.'.$v.',';<br>
}<br>
<br>
$upload_allow_ext = substr($upload_allow_ext, 0, -1);<br>
$upload_allow_desc = substr($upload_allow_desc, 0, -1);<br>
<br>
$this->assign('upload_allow_desc', $upload_allow_desc);<br>
$this->assign('upload_allow_ext', $upload_allow_ext);<br>
$this->display('read');<br>
}<br>
<br>
/* 初始化 */<br>
public function _initialize()<br>
{<br>
ignore_user_abort(true);<br>
set_time_limit($this->time_limit);<br>
<br>
// FLASHPAPER转换程序物理地址<br>
$this->flashpaper_url = $_SERVER['DOCUMENT_ROOT'].$this->flashpaper_url;<br>
<br>
// 自动创建转换保存目录<br>
@mkdir('.'.$this->swf_savepath);<br>
@mkdir('.'.$this->swf_savepath.date('Ym'));<br>
<br>
// SWF文件保存虚拟路径<br>
$this->swf_file_path = $this->swf_savepath.date('Ym');<br>
<br>
// SWF文件保存物理路径<br>
$this->swf_savepath = $_SERVER['DOCUMENT_ROOT'].$this->swf_savepath.date('Ym').'/';<br>
}<br>
<br>
/* 调用FlashPaper,$uploadlist是ThinkPHP的UPLOAD上传的数组形式的文件数组信息 */<br>
public function convertFile($uploadList)<br>
{<br>
<br>
if (!$uploadList){<br>
$uploadList = array( 0 => $_POST);<br>
$this->socket = true;<br>
}<br>
<br>
if(is_array($uploadList))<br>
{<br>
foreach ($uploadList as $v)<br>
{<br>
$this->scorce_file_path = $_SERVER['DOCUMENT_ROOT'].substr($v['savepath'],1).$v['savename'];<br>
$this->score_file_name = strrpos($v['savename'], '.') ? substr($v['savename'],0,strrpos($v['savename'], '.')) : $v['savename'];<br>
$this->scorce_file_ext = $v['extension'];<br>
$this->scorce_file_size = $v['size'];<br>
$this->scorce_attch_id = $v['attid'] ? $v['attid'] : 0;<br>
$this->scorce_save_path = substr($v['savepath'],1).$v['savename'];<br>
$this->execFile();<br>
}<br>
}<br>
<br>
}<br>
<br>
// 检测是否满足条件转换<br>
private function check()<br>
{<br>
$err = 0;<br>
$this->exec_error = '';<br>
$err_tip = array(<br>
1 => 'FlashPaper只能运行与Windows环境下',<br>
2 => 'FlashPrinter.exe转换文件不存在',<br>
3 => '没有exec函数执行权限',<br>
4 => 'FlashPrinter('.$this->flashpaper_url.')目录不可读',<br>
5 => '上传文件('.$this->scorce_file_path.')不存在',<br>
6 => '保存('.$this->swf_savepath.')目录不存在或不可写',<br>
7 => '不支持该文件格式('.$this->scorce_file_ext.')的转换',<br>
);<br>
<br>
// 是否WINDOWS环境<br>
if(!IS_WIN){<br>
$err = 1;<br>
}<br>
// 检测FlashPaper是否存在<br>
else if (!file_exists($this->flashpaper_url)){<br>
$err = 2;<br>
}<br>
// 检测exec是否可以执行<br>
else if (!function_exists('exec')){<br>
$err = 3;<br>
}<br>
// FlashPaper是否可读<br>
else if (!is_readable($this->flashpaper_url)){<br>
$err = 4;<br>
}<br>
// 源文件是否可读<br>
else if (!is_readable($this->scorce_file_path)){<br>
$err = 5;<br>
}<br>
// 保存目录是否可写<br>
else if (is_dir($this->swf_file_path)){<br>
$err = 6;<br>
}<br>
// 检测文件格式<br>
else if (!in_array($this->scorce_file_ext, explode('|', $this->allow_file_ext))){<br>
$err = 7;<br>
}<br>
<br>
if ($err){<br>
$this->exec_error = $err_tip[$err];<br>
return false;<br>
}<br>
<br>
return true;<br>
<br>
}<br>
<br>
// 转换上传文件<br>
private function execFile()<br>
{<br>
if ($this->check())<br>
{<br>
$swf_path = $this->swf_savepath.$this->score_file_name.'.swf ';<br>
<br>
// SWF文件相对路径<br>
$this->swf_file_path = $this->swf_file_path.'/'.$this->score_file_name.'.swf';<br>
<br>
// 转换SWF命令<br>
$command = $this->flashpaper_url.' -o '.$swf_path.$this->scorce_file_path;<br>
$command = str_replace("/","\\",$command);<br>
<br>
// 纪录转换时间<br>
$this->exec_start_time = $this->exectime();<br>
<br>
// 调用FlashPaper执行转换<br>
exec($command);<br>
<br>
$this->exec_end_time = $this->exectime();<br>
<br>
}<br>
<br>
// 保存执行信息<br>
$this->execSaveInfo();<br>
<br>
}<br>
<br>
// 记录转换运行数据<br>
private function execSaveInfo()<br>
{<br>
$state = 0; // 转换状态,默认未转换<br>
$time = time();<br>
$runtime = round($this->exec_end_time - $this->exec_start_time, 6);<br>
$scorcepath = $this->scorce_file_path;<br>
<br>
$swfread = M('swfread');<br>
<br>
if ($this->exec_error == '')<br>
{<br>
$sql = "INSERT INTO `log_swfread` (attid,filename,filesize,filetype,`time`,runtime,`state`,filepath,scorcepath)<br>
VALUES<br>
('{$this->scorce_attch_id}',<br>
'".$this->score_file_name.'.'.$this->scorce_file_ext."',<br>
'{$this->scorce_file_size}',<br>
'{$this->scorce_file_ext}',<br>
'{$time}',<br>
'{$runtime}',<br>
'{$state}',<br>
'{$this->swf_file_path}',<br>
'{$this->scorce_save_path}'<br>
);";<br>
}<br>
else<br>
{<br>
// 转换失败<br>
$sql = "INSERT INTO `log_swfread` (attid,filename,filesize,filetype,`time`,runtime,`state`,filepath,scorcepath,`error`)<br>
VALUES<br>
('{$this->scorce_attch_id}',<br>
'".$this->score_file_name.'.'.$this->scorce_file_ext."',<br>
'{$this->scorce_file_size}',<br>
'{$this->scorce_file_ext}',<br>
'{$time}',<br>
'{$runtime}',<br>
'0',<br>
'{$this->swf_file_path}',<br>
'{$this->scorce_save_path}',<br>
'{$this->exec_error}'<br>
);";<br>
}<br>
<br>
$swfread->execute($sql);<br>
$swfid = $swfread->query("SELECT MAX(swfid) as id FROM `log_swfread`");<br>
<br>
// socket模式:请求监听生成状态和执行时间<br>
if ($this->socket && $this->exec_error == ''){<br>
$i = 1;<br>
while(true){<br>
if (file_exists('.'.$this->swf_file_path)){<br>
$this->exec_end_time = $this->exectime();<br>
$runtime = round($this->exec_end_time - $this->exec_start_time, 6);<br>
$sql = "UPDATE log_swfread SET `state` = 1 , runtime = '{$runtime}' WHERE swfid = '{$swfid[0]['id']}'";<br>
$swfread->query($sql);<br>
break;<br>
}<br>
if (++$i > $this->time_limit-100) break;<br>
sleep(1);<br>
}<br>
}<br>
}<br>
<br>
// 执行时间<br>
private function exectime()<br>
{<br>
$time = explode(" ", microtime());<br>
$usec = (double)$time[0];<br>
$sec = (double)$time[1];<br>
return $sec + $usec;<br>
}<br>
}<br>
?>
AD:真正免费,域名+虚机+企业邮箱=0元