打印多个pdf放到压缩包中在浏览器中下载

根据模板打印多个pdf放到压缩包中并在浏览器下载

如何根据模板打印pdf可以看这个文章 :
链接: https://blog.csdn.net/weixin_34088838/article/details/94547120.

关于IText 的一些问题 :
链接: https://blog.csdn.net/yamadeee/article/details/83384071.

  @Override
    public void zipByteArrayOutputStream(HttpServletResponse response, List<ByteArrayOutputStream> baosList) throws IOException {
        //1.创建字节数组输出流,用于返回压缩后的输出流字节数组
        ServletOutputStream outputStream = response.getOutputStream();
        ZipOutputStream zipOut = new ZipOutputStream(outputStream);

        //zipOut.setEncoding("GBK");//设置编码格式,否则中文文件名乱码

        //3.遍历要批量压缩的集合文件流
        ByteArrayInputStream bais =null;
        Map<String, byte[]> tempMap =null;
        String fileName="印刷";
        int temp = 0 ;
        for (int i = 0; i < baosList.size(); i++) {
            //3.1将需要压缩的字节输出流,转为字节数组输入流,
            bais = new ByteArrayInputStream(baosList.get(i).toByteArray());

            //zipOut.setMethod(ZipOutputStream.DEFLATED);//进行压缩存储

            //zipOut.setLevel(Deflater.BEST_COMPRESSION);//压缩级别值为0-9共10个级别(值越大,表示压缩越利害)

            //3.2设置ZipEntry对象,并对需要压缩的文件命名
            if (i>0){
                zipOut.putNextEntry(new ZipEntry(fileName+"("+i+")"+".pdf"));
            }else {
                zipOut.putNextEntry(new ZipEntry(fileName+".pdf"));
            }


            //3.3读取要压缩的字节输出流,进行压缩
            temp = 0 ;
            while((temp=bais.read())!=-1){
                zipOut.write(temp) ;    // 压缩输出
            }

            // 3.4关闭流
            bais.close();
        }
        zipOut.flush();

        //先关压缩的输出流
        zipOut.close();
        outputStream.close();// 关闭流

    }

这个方法我传的参数是ByteArrayOutputStream类型的一个List ,也就是我已经打印好了的pdf的字节输出流,先循环打印出多个pdf把输出流放到List里面然后传给这个方法

需要注意的是一定要先关压缩的输出流

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值