java pdf 在线预览

我这里用的pdfjs做的前端分页

去官网下载pdfjs

https://mozilla.github.io/pdf.js/getting_started/#download

java pdf 在线预览

当前稳定版为2.0.943

下载完成后,解压

java pdf 在线预览

进入web文件夹

用浏览器打开viewer.html就可以看到pdf分页预览

 

代码

这里的pdf是写死的,

开发过程中,pdf要从后台读取

后台代码为,

/**
 * 文件预览
 */
@GetMapping(MODEL + "/readPdf.do")
public void displayPDF(HttpServletResponse response, @RequestParam("path") String path, @RequestParam("filename") String filename) {
    EmailAttachPath emailAttachPath = emailAttachPathService.queryCurrentFilePath();
    //附件路径
    try {
        //原文件
        File file = new File(emailAttachPath.getPath() + path);
        //解密之后的文件
        File file2 = new File(emailAttachPath.getPath() + path + filename);
        //解密文件
        FileEncDecUtil.encFile(file, file2);

        FileInputStream fileInputStream = new FileInputStream(file2);
        response.setHeader("Content-Disposition", "attachment;fileName=test.pdf");
        response.setContentType("multipart/form-data");
        OutputStream outputStream = response.getOutputStream();
        IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

js代码也要做修改

修改

java pdf 在线预览中的

java pdf 在线预览