[英]How to merge pdf files and download it in a zip file
I hope you can help me.我希望你能帮助我。 I have many pdf files, they are in a byte[] format, cause i got them from database.
我有很多 pdf 文件,它们是字节 [] 格式,因为我是从数据库中获取的。 I need merge them, but the resulted file can't has more than 1000 sheets, so if the total of my pdf files has more than 1000 sheets i'll have to build many pdf files limited in this size of sheets.
我需要合并它们,但生成的文件不能超过 1000 张,所以如果我的 pdf 文件总数超过 1000 张,我将不得不构建许多限制在这种纸张大小内的 pdf 文件。
I just done it, i built a algorithim that returns a List<ByteArrayInputStream[]>我刚刚完成,我构建了一个返回 List<ByteArrayInputStream[]> 的算法
Now, its my problem.现在,它是我的问题。 How can i get each file, merge them and put in a zip file for download?
如何获取每个文件,合并它们并放入一个 zip 文件以供下载?
I've tried this...我试过这个...
public static void openZipFile(HttpServletResponse response, String fileName, List<ByteArrayInputStream[]> conteudosZIP)
throws Exception {
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName.replaceAll("\u0020", "_").replaceAll(",", "_") + ".zip");
ServletOutputStream out = response.getOutputStream();
ZipOutputStream zout = new ZipOutputStream(out);
Integer cont = 1;
for(ByteArrayInputStream[] conteudoArray : conteudosZIP ) {
PDDocument result = new PDDocument();
PDFMergerUtility ut = new PDFMergerUtility();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
for(ByteArrayInputStream conteudo : conteudoArray) {
ut.appendDocument(result, PDDocument.load(conteudo));
}
result.save(byteArrayOutputStream);
ZipEntry ze = new ZipEntry(fileName + '_' + cont++ + ".pdf");
zout.putNextEntry(ze);
zout.write(byteArrayOutputStream.toByteArray());
zout.closeEntry();
}
}
It's not clear which part of this is not working for you.目前尚不清楚这其中的哪一部分对您不起作用。 What happens when you run this?
当你运行这个时会发生什么?
You may want to add a zout.close()
at the end.你可能想在最后添加一个
zout.close()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.