pdf文件在线预览

使用pdfjs技术实现PDF的在线预览功能。

1.官网下载pdf.js

2. 将下载下来的文件全部复制

3. js使用

function selPdf() {
//调用后台io流查看文件
window.open("../view/static/pdfjs/web/viewer.html?file="+encodeURIComponent("/pdf请求地址"),"pdf")

//测试使用文件
// window.open("../view/static/pdfjs/web/viewer.html?file=compressed.tracemonkey-pldi-09.pdf","pdf")
}

4. java IO流

@GetMapping("/pdf")
    public void FileInputStreamDemo(HttpServletResponse response){
        String path="";
        ServletOutputStream os =null;
        FileInputStream fis=null;
        try {
        os = response.getOutputStream();
        File file=new File(path);
        if(!file.exists()||file.isDirectory()) {
            throw new FileNotFoundException();
        }
        fis=new FileInputStream(file);
        byte[] buf = new byte[1024];
            while((fis.read(buf))!=-1){
                os.write(buf);
                buf=new byte[1024];//重新生成,避免和上次读取的数据重复
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fis!=null){fis.close();}
                if(os!=null){os.close();}
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }



如果想屏蔽下载打印按钮,可以在viewer.html中修改。

posted @ 2021-05-07 10:09  依旧~  阅读(427)  评论(0编辑  收藏  举报