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

一、单个PDF导出到浏览器

java 复制代码
    /**
     * 
     * @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 {
            //模板路径templatePath
            reader = 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输出浏览器

java 复制代码
 @Override
    public 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 文件到 ByteArrayOutputStream
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                //模板路径
                String templatePath = templateProperties.getPath() + "\\weighbridge_record.pdf";
                generateTempPDF(templatePath, fileName, data, null, bos);

                // 将生成的 PDF 文件写入 ZIP
                ZipEntry 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);
        }
    }
java 复制代码
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();
            }
        }
    }
相关推荐
南宫生12 分钟前
力扣每日一题【算法学习day.133】
java·学习·算法·leetcode
獨枭14 分钟前
如何在 Mac 上安装并配置 JDK 环境变量
java·macos·jdk
m0_7383556926 分钟前
java泛型
java·开发语言
web2u30 分钟前
Docker入门及基本概念
java·运维·服务器·spring·docker·容器
qq_2187533141 分钟前
常用Git命令
java·git
十步杀一人_千里不留行1 小时前
React Native 下拉选择组件首次点击失效问题的深入分析与解决
javascript·react native·react.js
计算机小白一个1 小时前
蓝桥杯 Java B 组之背包问题(01背包、完全背包)
java·职场和发展·蓝桥杯
计算机毕设定制辅导-无忧学长1 小时前
Maven 基础环境搭建与配置(二)
java·maven
逸狼1 小时前
【JavaEE进阶】Spring IoC
java·spring·java-ee
d3soft1 小时前
厦大团队:DeepSeek大模型概念、技术与应用实践 140页PDF完整版下载
ai·pdf·教程·deepseek