PDF.js的简单使用

1.后端返回文件流,可在浏览器访问显示pdf的请求路径。

     例如:http://localhost:8081/showFile?path=C:/Users/lhl/Desktop/files/pdf/b3c39656ea744095bd9eb52d48c51803.pdf这个可以在浏览器显示pdf....

 直接的话是调用的window的pdf。我想要隐藏下载和打印。做不到。

后端代码:

    

    /**
     * 展示文件,主要是先读取,再输出
     * @param path
     * @param response
     */
    @RequestMapping(value = "/showFile")
    public void showFile(
            @RequestParam(name = "path") String path, HttpServletResponse response,
            HttpServletRequest request) {
        String pdfPath="C:/Users/lhl/Desktop/files/pdf/b3c39656ea744095bd9eb52d48c51803.pdf";
        String requestURI = request.getRequestURI();
        StringBuffer requestURL = request.getRequestURL();
        String contextPath = request.getContextPath();
        String servletPath = request.getServletPath();
        System.out.println(path);
        //header中的这个*指所有域名,参数指的是域名。。可以指定允许特定的域名
        response.setHeader("Access-Control-Allow-Origin", "*");
        File file = new File(pdfPath);
        System.out.println("file"+file.exists());
        System.out.println("----------");
        if (file.exists()) {
            System.out.println("文件存在");
            byte[] buffer = new byte[2048];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                OutputStream outputStream = response.getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    outputStream.write(buffer, 0, i);
                    i = bis.read(buffer);
                }

                bis.close();
                outputStream.close();
                fis.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else {
            System.out.println("文件不存在---------------------------");
        }

    }

 

 

2.下载pdf.js  http://mozilla.github.io/pdf.js/

     l

其中viewer.html访问会出现默认的pdf.

原路径?file=你的pdf文件。即可达到要求。

 

关于隐藏上面的按钮,直接在view.html找到相应的按钮,style=display:none即可,也可以在上面写button或者a链接进行返回的操作。

你会遇到错误;

开修改web下的viewer.js, 将DEFAULT_URL的值设为空字符串‘’,这样就能通过传参设置你想看的pdf文件了,

遇到:file origin does not match viewer's错误;

解决方案:webViewerInitialized()中注释验证url的函数://validateFileURL(file);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值