Reputation: 25
I hope you can help me. I have many pdf files, they are in a byte[] format, cause i got them from database. 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.
I just done it, i built a algorithim that returns a List<ByteArrayInputStream[]>
Now, its my problem. How can i get each file, merge them and put in a zip file for download?
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();
}
}
Upvotes: 1
Views: 678
Reputation: 648
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.
Upvotes: 1