itext 7批量生成pdf文件并以压缩包形式下载

itext 7批量生成pdf文件并以压缩包形式下载

引入jar

<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itext7-core</artifactId>
			<version>7.0.3</version>
			<type>pom</type>
		</dependency>

代码实现–生成pdf文件

    /**
     * 生成pdf文件
     * @return java.io.ByteArrayOutputStream
     * @date 2023/6/25 16:29
     * @author fyh
     **/
    private ByteArrayOutputStream exportPdf(){
        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            PageSize pageSize = PageSize.A4.rotate();
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(out));
            Document doc = new Document(pdfDoc, pageSize);
            float[] f = new float[]{0.01f};
            Table table = new Table(UnitValue.createPercentArray(f));
            table.setWidth(745);
            Cell cell = new Cell();
            //设置表格边框颜色
            cell.setBorder(new SolidBorder(new DeviceRgb(0xFFF, 0xFFF, 0xFFF),3));
            PdfFont font = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H",true);
            //背景图上写入汉字
            Paragraph p = new Paragraph("努尔哈赤")
                    .setFont(font).setFontSize(13f).setFontColor(DeviceGray.BLACK);
            //写入汉字坐标
            p.setFirstLineIndent(140f);
            p.setFixedLeading(418f);
            cell.add(p);
            //获取表格背景图
            URL resource = this.getClass().getClassLoader().getResource("honour.png");
            assert resource != null;
            Image img = new Image(ImageDataFactory.create(resource.getPath()));

            cell.setNextRenderer(new ImageBackgroundCellRenderer(cell, img));
            cell.setHeight(590 * img.getImageHeight() / img.getImageWidth());

            table.addCell(cell);
            doc.add(table);
            doc.close();
            out.flush();
            return out;
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return new ByteArrayOutputStream();
    }

代码实现–生成并下载压缩包

    /**
     *生成压缩包
     * @date 2023/6/25 16:02
     * @author fyh
     **/
    private void downLoadZip(List<Map<String,Object>> fileList, HttpServletResponse httpResponse){
        try(ZipOutputStream zipOutputStream = new ZipOutputStream(httpResponse.getOutputStream())) {
            //下载压缩包
            httpResponse.setContentType("application/zip");
            httpResponse.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("附件.zip", "UTF-8"));
            // 创建 ZipEntry 对象
            for (Map<String,Object> map:fileList){
                ZipEntry zipEntry =  new ZipEntry((String) map.get("fileName"));
                zipOutputStream.putNextEntry(zipEntry);
                zipOutputStream.write((byte[]) map.get("outByte"));
            }
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        }
    }

controller层 代码实现

    /**
     * 导出压缩包
     * @date 2023/6/25 16:15
     * @author fyh
     **/
    @GetMapping(value = "downLoadZip")
    public void downLoadZip2(HttpServletResponse httpServletResponse){
        //生成的文件
        ByteArrayOutputStream byteArrayOutputStream = exportPdf();
        Map<String,Object> excelOut = new HashMap<>();
        excelOut.put("fileName","努尔哈赤.pdf");
        excelOut.put("outByte",byteArrayOutputStream.toByteArray());
        List<Map<String,Object>> fileList = new ArrayList<>();
        fileList.add(excelOut);
        //文件压缩为zip
        downLoadZip(fileList,httpServletResponse);
    }

实现效果图

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值