根据模板打印多个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里面然后传给这个方法
需要注意的是一定要先关压缩的输出流