当前位置: 首页 > news >正文

根据PDF模板单个PDF导出到浏览器和多个PDF打包ZIP导出到浏览器

一、单个PDF导出到浏览器
 

    /*** * @param templatePath 模板路径* @param fileName 文件名称* @param data 填充文本* @param images 填充图片* @param response* @throws IOException*/public static void generateTempPDF(String templatePath, String fileName, Map<String, String> data , Map<String, String> images, HttpServletResponse response) throws IOException {fileName = URLEncoder.encode(fileName, "UTF-8");// 设置响应头response.setContentType("application/force-download");response.setHeader("Content-Disposition","attachment;fileName=" + fileName);PdfReader reader = null;PdfStamper ps = null;OutputStream fos = null;ByteArrayOutputStream bos = null;try {//模板路径templatePathreader = new PdfReader(templatePath);bos = new ByteArrayOutputStream();ps = new PdfStamper(reader, bos);// 使用中文字体BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);ArrayList<BaseFont> fontList = new ArrayList<>();fontList.add(bf);AcroFields fields = ps.getAcroFields();fields.setSubstitutionFonts(fontList);fillData(fields, data);//渲染//插入图片if (EmptyUtils.isNotEmpty(images)) {fillImage(fields, ps,images);}//必须要调用这个,否则文档不会生成的ps.setFormFlattening(true);if(ps != null){ps.close();}// 保存到本地// fos = new FileOutputStream(exportPath);// 输出到浏览器端fos = response.getOutputStream();fos.write(bos.toByteArray());}catch (Exception e){e.printStackTrace();}finally {if(fos!=null){fos.flush();fos.close();}if (bos != null){bos.close();}if(reader != null){reader.close();}}}

二、多个PDF文件压缩ZIP输出浏览器

 @Overridepublic void exportZip(List<Long> ids, HttpServletResponse response) throws IOException {response.setContentType("application/zip");response.setHeader("Content-Disposition", "attachment; filename=weighbridge_records.zip");try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {for (Long id: ids) {// 业务代码String fileName = aaa.pdf;Map<String, String> data = new HashMap<>();// 生成 PDF 文件到 ByteArrayOutputStreamByteArrayOutputStream bos = new ByteArrayOutputStream();//模板路径String templatePath = templateProperties.getPath() + "\\weighbridge_record.pdf";generateTempPDF(templatePath, fileName, data, null, bos);// 将生成的 PDF 文件写入 ZIPZipEntry zipEntry = new ZipEntry(fileName);zos.putNextEntry(zipEntry);zos.write(bos.toByteArray());zos.closeEntry();bos.close();}} catch (IOException e) {e.printStackTrace();} catch (DocumentException e) {throw new RuntimeException(e);}}
public static void generateTempPDF(String templatePath, String fileName, Map<String, String> data, Map<String, String> images, ByteArrayOutputStream bos) throws IOException, DocumentException {PdfReader reader = null;PdfStamper ps = null;try {reader = new PdfReader(templatePath);ps = new PdfStamper(reader, bos);BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);ArrayList<BaseFont> fontList = new ArrayList<>();fontList.add(bf);AcroFields fields = ps.getAcroFields();fields.setSubstitutionFonts(fontList);fillData(fields, data); // 渲染// 插入图片if (EmptyUtils.isNotEmpty(images)) {fillImage(fields, ps, images);}ps.setFormFlattening(true);} catch (Exception e) {e.printStackTrace();} finally {if (ps != null) {ps.close();}if (reader != null) {reader.close();}}}

http://www.lryc.cn/news/502569.html

相关文章:

  • 如何创建一个基本的Spring Boot应用程序
  • 1.2 计算机网络的分类和应用(重要知识点)
  • @JsonSerialize失效解决
  • Docker部署WebRTC-Streamer
  • 2025年的大模型计划重点在于跨领域智能、工作流自动化、多模态能力强化
  • day12 接口测试 ——入门→精通→实战(1)
  • 伏羲0.07(文生图)
  • scala的泛型特质的应用场景
  • Win10环境vscode+latex+中文快速配置
  • 【vue2】el-select,虚拟滚动(vue-virtual-scroller)
  • 【ETCD】[源码阅读]深度解析 EtcdServer 的 processInternalRaftRequestOnce 方法
  • 【RabbitMQ】RabbitMQ中核心概念交换机(Exchange)、队列(Queue)和路由键(Routing Key)等详细介绍
  • 【AI知识】过拟合、欠拟合和正则化
  • 计算机毕设-基于springboot的航空散货调度系统的设计与实现(附源码+lw+ppt+开题报告)
  • 视图、转发与重定向、静态资源处理
  • 优选算法——分治(快排)
  • 【Linux系统】文件系统
  • javaweb的基础
  • 家里养几条金鱼比较好?
  • 写作词汇积累:差池、一体两面、切实可行极简理解
  • 移远EC200A-CN的OPENCPU使用GO开发嵌入式程序TBOX
  • LEED绿色建筑认证最新消息
  • SpringBoot中集成常见邮箱中容易出现的问题
  • webstorm开发uniapp(从安装到项目运行)
  • C# 探险之旅:第七节 - 条件判断(三元判断符):? : 的奇妙冒险
  • FlinkCDC实战:将 MySQL 数据同步至 ES
  • debug小记
  • Qt C++ 显示多级结构体,包括结构体名、变量名和值
  • 【JAVA】旅游行业中大数据的使用
  • 【AI+网络/仿真数据集】1分钟搭建云原生端到端5G网络