简体   繁体   English

如何合并pdf文件并将其下载为zip文件

[英]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.

相关问题 如何使用 Liferay 将多个文件下载为 zip 文件? - How to download multiple files as a zip-file using Liferay? 如何在wicket中使用zip文件下载更大的文件? - How to use a zip file download in wicket for larger files? 在服务器(硬盘)上未创建ZIP文件的情况下,如何让客户端下载包含自定义选定文件的ZIP文件? - Without creating ZIP file on server (hard disk) how to let client download a ZIP file with custom selected files? 如何在下载时将单个选定的PDF文件合并为一个PDF? - How can I merge individual selected PDF files into one PDF upon download? Generate Zip File - download PDF from Url and Generate Zip to Download in Browser - Broken PDF - Generate Zip File - download PDF from Url and Generate Zip to Download in Browser - Broken PDF 如何创建zip文件并下载? - How to create a zip file and download it? 压缩大量pdf文件并下载:OutOfMemoryError:Java堆空间 - Zip a great number of pdf file and download it: OutOfMemoryError: Java heap space 以zip下载所有文件 - download all files as zip 如何读取zip文件中的文件? - How to read the files in the zip file? 如何在geb中将zip文件下载到当前目录? - How In geb download zip file into the current directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM