背景:项目中实现pdf文件的预览以及下载

  • 环境:jdk1.8、SpringBoot2.0
  • PDF.js下载地址将下载的源码拷入项目中
  • 修改viewer.js:
将defaultUrl: {value: 'compressed.tracemonkey-pldi-09.pdf',---此处是默认的pdf的路径kind: OptionKind.VIEWER}修改为:defaultUrl: {value: '',kind: OptionKind.VIEWER}
  • 打开新窗口预览:
     <input type="button" value="预览" id="viewBtn"><script type="text/javascript">$("#viewBtn").click(function() {var curWwwPath = window.document.location.href;var pathName = window.document.location.pathname;var pos = curWwwPath.indexOf(pathName);var localhostPath = curWwwPath.substring(0, pos);window.open(localhostPath+ "/js/pdf/web/viewer.html?file="+ encodeURIComponent("/preview?fileName=0602.pdf"));});//后台controller代码,根据前端传入的fileName到指定目录读取pdf文件,进行展示@RequestMapping(value = "/preview", method = RequestMethod.GET)public void prePDF(String fileName, HttpServletRequest request, HttpServletResponse response) {logger.info("文件名:" + fileName);File file = new File("E:/pdf/" + fileName);if (file.exists()) {byte[] data = null;try {FileInputStream input = new FileInputStream(file);data = new byte[input.available()];input.read(data);response.getOutputStream().write(data);input.close();} catch (Exception e) {logger.info("pdf文件处理异常...");}}}
  • PDF文件下载:
<input type="button" value="下载" id="download">
$("#download").click(function() {var form = $("<form>");form.attr("style", "display:none");form.attr("target", "");form.attr("method", "post");//提交方式为postform.attr("action", "/downloadFile");//定义action$("body").append(form);form.submit();});
//后台代码
@RequestMapping(value="/downloadFile")public void downloadFile(HttpServletResponse response) {String downloadFilePath = "E:/pdf/";   //被下载的文件在服务器中的路径,String fileName = "0602.pdf";           //被下载文件的名称File file = new File(downloadFilePath+fileName);if (file.exists()) {response.setContentType("application/force-download");// 设置强制下载不打开            response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);byte[] buffer = new byte[1024];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);}} catch (Exception e) {e.printStackTrace();} finally {if(bis != null) {try {bis.close();}catch(IOException e) {e.printStackTrace();}}if(fis != null) {try {fis.close();}catch(IOException e) {e.printStackTrace();}}}}}
  • 预览页面顶部会有相应的工具栏,打印、下载、翻页、放大等,根据个人实际需要,可以查看页面源码,在viewer.html中注释掉相应的功能.

  • 也可以通过pdfbox读取PDF文件内容:

引入jar包:
<!-- PDF读取依赖 --><dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.4</version></dependency>

后台读取pdf文件代码:

public String viewPDF(String proCode,String fileName,String originPage, HttpServletRequest request) {request.setAttribute("dse_sessionId", request.getParameter("dse_sessionId").trim());logger.info("披露报告预览文件名:" + fileName);File file = new File(constant.getExposeLocalDir() + fileName);if (!file.exists()) { // 文件不存在,则 从FTP下载文件到本地}}//读取pdf文件内容-代码实现try {PDDocument document = PDDocument.load(file);document.getClass();if(!document.isEncrypted()) {PDFTextStripperByArea stripper = new PDFTextStripperByArea();stripper.setSortByPosition(true);PDFTextStripper textStripper = new PDFTextStripper();String exposeContent = textStripper.getText(document);String[] content = exposeContent.split("\\n");StringBuffer stringBuffer = new StringBuffer();for(String line:content) {stringBuffer.append(line); }}} catch (Exception e) {logger.info("读取pdf文件异常...");}return "";}

Springboot实现PDF预览、下载、读取相关推荐

  1. SpringBoot+FreeMarker+flying-saucer-pdf实现PDF预览、分页需求

    文章目录 SpringBoot+FreeMarker+flying-saucer-pdf实现PDF预览.分页需求 需求说明 程序示例 程序示例说明 添加依赖包 FreeMarker模板文件编写 工具类 ...

  2. PDF 预览和下载你是怎么实现的?

    在开发过程中要求对 PDF 类型的发票提供 预览 和 下载 功能,**PDF** 类型文件的来源又包括 H5 移动端 和 **PC 端**,而针对这两个不同端的处理会有些许不同,下文会有所提及. 针对 ...

  3. vue 项目中实现pdf预览 pdf打印 pdf下载

    在Vue项目中实现PDF预览.打印和下载可以通过以下步骤来实现: 安装pdf.js pdf.js是一个JavaScript库,可以用于在Web上渲染PDF文件. 可以使用npm安装pdf.js,命令如 ...

  4. java pdf文件下载_Java后台返回PDF文件预览下载

    Java后台返回PDF文件预览下载 开始的代码的系统代码为: ResponseEntity onePdf = s3Wrapper.download(contractEntity.getContract ...

  5. 【iframe结合pdf.js实现pdf的预览/下载及打印功能】

    iframe结合pdf.js实现pdf的预览/下载及打印 1.[下载pdf.js](http://mozilla.github.io/pdf.js/getting_started/#download) ...

  6. SpringBoot:文件下载、pdf预览、文件压缩

    文件下载 编写工具类,编辑http响应体格式.其中Content-type为application/octet-stream代表二进制流,为下载文件的格式.详见对照表:https://tool.osc ...

  7. 钉钉小程序上传预览下载word,pdf文档等一系列问题

    钉钉小程序上传预览下载word,pdf文档等一系列问题 小程序"用完即走"的理念使得有着很好的发展市场,不仅微信小程序,支付宝,钉钉小程序等都受用户的喜爱. 刚接触了一个企业内部应 ...

  8. vue的PDF预览插件(vue-pdf),支持旋转、放大缩小、打印、下载等

    PDF预览插件 1.安装插件(vue-pdf) 2.引用注册pdf组件 3.pdf 常用属性与方法调用 4.常见报错 1.安装插件(vue-pdf) npm i pdfjs-dist@2.5.207 ...

  9. itext5预览/下载/字节流方式生成PDF

    原文链接:https://www.cnblogs.com/waft/p/15603802.html Java itext5预览/下载/字节流方式生成PDF,并设置页眉页脚,html识别,行首标点长数字 ...

最新文章

  1. python基础看什么书-python新手看什么书比较好?这五本最靠谱
  2. 推荐系统里,可以用蒸馏吗?
  3. linux /bin/sh -c的用途
  4. 如何和何时使用 CSS 的权重设置 !important (建议:永不使用!)
  5. 数据定义语言DDL之数据库操作
  6. lua qt 编译_Win32下 Qt与Lua交互使用(一):配置Qt下Lua运行环境
  7. 什么是次梯度(次导数)
  8. 清北学堂2018年1月省选强化班模拟考试1
  9. MFC中将view内容保存为bmp
  10. Linux网络编程:使用select函数实现socket 收发数据
  11. 推荐系统之协同过滤算法
  12. 找101-200之间的素数(Java实现)
  13. R语言使用median函数计算向量数据的中位数
  14. 野火ISO-V2学习
  15. 扫雷小游戏 3.0 版本
  16. OneDrive文件数限制:150000
  17. 11.1 p值的意义
  18. 手机耳塞 录音同时外放_如何将Android手机切换为“单声道”(这样就可以戴一副耳塞)
  19. MySQL忘记密码修改密码
  20. C++:Error C 1010:在查找预编译头时遇到意外的文件结尾。

热门文章

  1. FTP和使用Docker搭建FTP服务
  2. 京东商智--POP店铺数据导出(每一个类目,每一个月份的数据)--本次为了汇总2019年度数据
  3. GAN生成式对抗网络
  4. 台式计算机电池更换后无法启动,如何解决电脑换主板电池后开不了机了
  5. linux 随机10字符病毒,Linux 10字符串命令病毒的处理记录
  6. css3新增属性有哪些?css3中常用的新增属性总结
  7. inout port 仿真
  8. 19种最佳HTML5和JavaScript游戏引擎和模板
  9. Referrer和Referrer Policy介绍
  10. puzhong 51矩阵键盘短按,长按数码管加减数