使用FlexPaperView在线阅读PDF

需要

FlexPaperViewer.swf

pdf2swf.exe

jsp部分

art.dialog.open(getRootPath() + '/contract/contract!lookContractPDF.xhtml?conid='+conid,{title: '合同PDF阅读窗', width: 750, height: "100%"});


显示PDF的JSP代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.hwxx.utils.CConst"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
</head>
 <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/style/flexPaper/flexpaper.css" />
 <script type="text/javascript" src="${pageContext.request.contextPath}/script/common/utils.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/script/flexPaper/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/script/flexPaper/flexpaper.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/script/flexPaper/flexpaper_handlers.js"></script>

<body>

    <div id="documentViewer" class="flexpaper_viewer" style="width:100%;height:842px"></div>

        <script type="text/javascript">
              $('#documentViewer').FlexPaperViewer(
                { config : {
//下面为PDF文件的.swf文件的地址
                SWFFile :'${pageContext.request.contextPath}'+'/images/swf/'+'${pdfname}.swf',
                Scale :1,
                ZoomTransition : 'easeOut',
                ZoomTime : 0.5,
                ZoomInterval : 0.2,
                FitPageOnLoad : true,
                FitWidthOnLoad : false,
                FullScreenAsMaxWindow : false,
                ProgressiveLoading : false,
                MinZoomSize : 0.2,
                MaxZoomSize : 5,
                SearchMatchAll : false,
                InitViewMode : 'Portrait',
                RenderingOrder : 'flash',
                StartAtPage : '',
                ViewModeToolsVisible : true,
                ZoomToolsVisible : true,
                NavToolsVisible : true,
                CursorToolsVisible : true,
                SearchToolsVisible : true,
                WMode : 'window',
                localeChain: 'en_US'
            }}
    );
            
        </script>
    
</body>
</html>

java后台

FlexPaperView阅读时是调取.swf文件,所以在上传pdf时最好转换一份成swf文件

上传并转换的代码

/**
     * 上传合同PDF文件
     * @return
     * @throws IOException 
     */
    public void uploadPDF() throws IOException{
        

        String contractid = reqParam("contractid");
        String fileInputFileName = reqParam("fileInputFileName");
        ActionContext ctx = ActionContext.getContext();
        HttpServletResponse sd = ServletActionContext.getResponse();
         sd.setContentType("UTF-8");
         String path= request.getSession().getServletContext().getRealPath("images/uploadPDF");
           File fd=new File(path);  
           
           if(!fd.exists()){
              fd.mkdir();
           }
           try {
               DateFormat formater = new SimpleDateFormat("yyyyMMddHHmmss");
               String picname = formater.format(new Date());
               String savefile = null;
                
               Contract contract = contractService.findbyid(getInt(contractid));
               
                //保存图片信息
                if(file!=null){
                    savefile = picname + fileInputFileName.substring(fileInputFileName.lastIndexOf("."));
                    FileUtils.copyFile(file, new File(fd ,savefile));
                  //把pdf文件转为swf文件
                    Pdf2SwfUtil.pdf2swf(request, savefile);
                    contract.setPdfname(picname);
                }
                
               
                //根据conid找出对应合同,写入合同信息
               
                contract.setModifier(getEmpNum());
                contract.setModifiertime(DateUtil.getDateNow("yyyy-MM-dd"));
                recordLog("上传合同PDF文件-[合同编号:"+contract.getContractlabel()+"]");
                contractService.update(contract);
                
                JsonObject jo = new JsonObject();
                   jo.addProperty("success",true);
                   jo.addProperty("msg","上传成功");
                   Gson gson = new Gson();
                   System.out.println(gson.toJson(jo));
                   response.setContentType("text/html;charset=utf-8");
                   response.getWriter().write(gson.toJson(jo));
                
           } catch (IOException e) {
                   JsonObject jo = new JsonObject();
                  jo.addProperty("success",true);
                  jo.addProperty("msg","上传成功");
                  Gson gson = new Gson();
                  System.out.println(gson.toJson(jo));
                  response.setContentType("text/html;charset=utf-8");
                  response.getWriter().write(gson.toJson(jo));
               e.printStackTrace();
           }
           
        
        
    
        
        
    }

   Pdf2SwfUtil工具类

public class Pdf2SwfUtil {  

    /**
     * 将pdf文件转换为swf文件
     * @param name
     * @return
     * @throws IOException 
     */
    public static boolean pdf2swf(HttpServletRequest request, String name)
    {
        try
        {
            //获取 pdf文件夹的路径
            String pdfPath = getDirectory(request, "images\\uploadPDF");
            //获取pdf文件夹中的某个文件路径
            String pdfFile = pdfPath.concat("\\" + name);
            //获取swf文件夹的路径
            String swfPath = getDirectory(request, "images\\swf");
            //获取swf文件夹中的某个文件路径
            String swfFile = swfPath.concat("\\" + name);
            //调用tools文件夹中的pdf2swf工具
            String toolsPath = getDirectory(request, "SWFTools");
            Runtime run = Runtime.getRuntime();
            Process process = run.exec(toolsPath+ "\\pdf2swf.exe  -t  " + pdfFile
                    + "   -o   " + swfFile.replace(".pdf",".swf")+" -T 9 ".replace("Program Files","'Program Files'"));
            process.waitFor();
            return true;
        } catch (Exception e)
        {
            return false;
        }  

    }

    /**
     * 获取某个文件夹的路径
     * @param request
     * @param directoryName
     * @return
     */
    public static String getDirectory(HttpServletRequest request, String directoryName)
    {
        String directory = request.getSession().getServletContext()
                .getRealPath(directoryName);
        return directory;
    }


}


转载于:https://my.oschina.net/u/1765449/blog/272332

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值