PHP结合PDF2SWF插件实现在线PDF预览

    因为工作需要所以要写一个PDF在线预览,老规矩先百度、google一番搜索总算找到一个方法。

function pdf2swf($arr,$path,$flag = ''){
     $swf_path        =    $path;//上传路径
     $swf_file_path    =     $swf_path.date("Y",$arr['create_datetime']).DIRECTORY_SEPARATOR.date("m",$arr['create_datetime']).DIRECTORY_SEPARATOR.date("d",$arr['create_datetime']).DIRECTORY_SEPARATOR.'pdf2swf'.DIRECTORY_SEPARATOR.$arr['guid'].DIRECTORY_SEPARATOR; //生成唯一的pdf路径 guid 相当于uniqid函数
    //没有的话就创建这个目录
    if(!$flag){ 
        create_path($swf_file_path);
    }else{
        delAll($swf_file_path);
    }            
    $pdf_file    =    $path.date("Y",$arr['create_datetime'])."/".date("m",$arr['create_datetime'])."/".date("d",$arr['create_datetime'])."/".$arr["file"]; //取得pdf地址
    $cmd_pdf2swf = "D:/SWFTools/pdf2swf.exe $pdf_file -o $swf_file_path%.swf"; //执行这个命令 把pdf每页转成swf格式的文件
    $exec = exec($cmd_pdf2swf);
    @copy($swf_path.'data.xml',$swf_file_path."/".'data.xml');
    @copy($swf_path.'index.html',$swf_file_path."/".'index.html');
    @copy($swf_path.'magazine.swf',$swf_file_path."/".'magazine.swf'); //这个是swf的播放器
    $file_sum    = getSum($swf_file_path);
    $xml    =    '<cXmag><pagewidth>720.000000</pagewidth><pageheight>900.000000</pageheight><pagecount>'.($file_sum-3).'</pagecount><pagedir>.</pagedir><logo>[logopic]</logo><flipsound>[default]</flipsound><bgsound></bgsound><righttoleft>0</righttoleft></cXmag>';
    $xml    .=    '</content>';
    
    file_put_contents($swf_file_path."/"."setting.xml", $xml);
}
//删除指定目录下所有文件
function delAll($dir){        
    $handle=dir($dir);
    while($entry=$handle->read()){
        if(($entry!=".")&&($entry!="..")){
            $entry = $dir."/".$entry;
            if(!is_dir($entry)){
                unlink($entry);
            }
        }
    }
}

//统计指定目下的文件总数
function getSum($dir){
    $file_sum    =    0;
    $handle=dir($dir);
    while($entry=$handle->read()){
        if(($entry!=".")&&($entry!="..")){
            if(!is_dir($dir."/".$entry)){
                $file_sum++;
            }
        }
    }
    return $file_sum;
}
//创建目录
function create_path($path) {
    $path_array = explode ( DIRECTORY_SEPARATOR, $path );
    $temp = array ();
    $dir = '';
    foreach ( $path_array as $index => $path_item ) {
        array_push ( $temp, array_pop ( $path_array ) );
        $dir = implode ( DIRECTORY_SEPARATOR, $path_array );
        if (is_dir ( $dir )) {
            break;
        }
    }
    $temp = array_reverse ( $temp );
    foreach ( $temp as $temp_path ) {
        $dir .= DIRECTORY_SEPARATOR . $temp_path;
        create_dir ( $dir );
    }

}

这个是显示页面代码

<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>在线阅读</title>
<meta name="Description" content="Flash Page Flip is best advanced dynamic page flip flash object">
<meta name="Keywords" content="page flip, page flipping, page turn, flip album, flipping book, digital book, online catalog">
<style type="text/css">
<!--
body {
    background-color: #ccc;
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
}
-->
</style></head>
<body><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="100%" height="100%">
  <param name="movie" value="<?php echo $obj['info']['path'].date("Y",$obj['info']['rows_sucai']['create_datetime']).'/'.date("m",$obj['info']['rows_sucai']['create_datetime']).'/'.date("d",$obj['info']['rows_sucai']['create_datetime']).'/pdf2swf/'.$obj['info']['rows_sucai']['id'].'/' ?>magazine.swf" />
  <param name="quality" value="high" />
  <param name="bgcolor" value="#cccccc" />
  <param name="allowFullScreen" value="true" />
  <param name="allowScriptAccess" value="sameDomain" />
  <embed src="<?php echo $obj['info']['path'].date("Y",$obj['info']['rows_sucai']['create_datetime']).'/'.date("m",$obj['info']['rows_sucai']['create_datetime']).'/'.date("d",$obj['info']['rows_sucai']['create_datetime']).'/pdf2swf/'.$obj['info']['rows_sucai']['id'].'/' ?>magazine.swf" width="1000px" height="1000px" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="window" allowFullScreen="true" allowScriptAccess="sameDomain"></embed>
</object></body></html>

我把程序需要到的文件都发到百度网盘去了,需要的可以去下载

http://pan.baidu.com/s/1pHRku

转载于:https://www.cnblogs.com/nethao/p/phppdf.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值