根据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();
            }
        }
    }
相关推荐
前端付豪9 分钟前
1、震惊!99% 前端都没搞懂的 JavaScript 类型细节
前端·javascript·面试
朝与暮10 分钟前
js符号(Symbol)
前端·javascript
Java水解36 分钟前
JAVA经典面试题附答案(持续更新版)
java·后端·面试
大怪v1 小时前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
洛小豆3 小时前
在Java中,Integer.parseInt和Integer.valueOf有什么区别
java·后端·面试
遂心_3 小时前
为什么 '1'.toString() 可以调用?深入理解 JavaScript 包装对象机制
前端·javascript
王同学QaQ3 小时前
Vue3对接UE,通过MQTT完成通讯
javascript·vue.js
前端小张同学3 小时前
服务器上如何搭建jenkins 服务CI/CD😎😎
java·后端
ytadpole3 小时前
Spring Cloud Gateway:一次不规范 URL 引发的路由转发404问题排查
java·后端
华仔啊3 小时前
基于 RuoYi-Vue 轻松实现单用户登录功能,亲测有效
java·vue.js·后端