根据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();
            }
        }
    }
相关推荐
gnip7 小时前
企业级配置式表单组件封装
前端·javascript·vue.js
掘金安东尼9 小时前
抛弃自定义模态框:原生Dialog的实力
前端·javascript·github
皮皮林55110 小时前
IDEA 源码阅读利器,你居然还不会?
java·intellij idea
hj5914_前端新手13 小时前
javascript基础- 函数中 this 指向、call、apply、bind
前端·javascript
Hilaku13 小时前
都2025年了,我们还有必要为了兼容性,去写那么多polyfill吗?
前端·javascript·css
LuckySusu13 小时前
【js篇】JavaScript 原型修改 vs 重写:深入理解 constructor的指向问题
前端·javascript
LuckySusu13 小时前
【js篇】如何准确获取对象自身的属性?hasOwnProperty深度解析
前端·javascript
LuckySusu14 小时前
【js篇】深入理解 JavaScript 作用域与作用域链
前端·javascript
LuckySusu14 小时前
【js篇】call() 与 apply()深度对比
前端·javascript
LuckySusu14 小时前
【js篇】addEventListener()方法的参数和使用
前端·javascript