gpt4 book ai didi

java - iText - OutOfMemory 创建超过 1000 个 PDF

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:08:05 26 4
gpt4 key购买 nike

我想创建一个充满 PDF-As 的 ZipOutputStream。我正在使用 iText(版本 5.5.7)。对于超过 1000 个 pdf 条目,我在 doc.close() 上遇到 OutOfMemory 异常并且找不到泄漏。

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(baos));
zos.setEncoding("Cp850");
for (MyObject o : objects) {
try {
String pdfFilename = o.getName() + ".pdf";
zos.putNextEntry(new ZipEntry(pdfFilename));
pdfBuilder.buildPdfADocument(zos);
zos.closeEntry();
} ...

PDF生成器

public void buildPdfADocument(org.apache.tools.zip.ZipOutputStream zos){
Document doc = new Document(PageSize.A4);
PdfAWriter writer = PdfAWriter.getInstance(doc, zos, PdfAConformanceLevel.PDF_A_1B);
writer.setCloseStream(false); // to not close my zos
writer.setViewerPreferences(PdfWriter.ALLOW_PRINTING | PdfWriter.PageLayoutSinglePage);
writer.createXmpMetadata();
doc.open();
// adding Element's to doc
// with flushContent() on PdfPTables
InputStream sRGBprofile = servletContext.getResourceAsStream("/WEB-INF/conf/AdobeRGB1998.icc");
ICC_Profile icc = ICC_Profile.getInstance(sRGBprofile);
writer.setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
//try to close/flush everything possible
doc.close();
writer.setXmpMetadata(null);
writer.flush();
writer.close();
if(sRGBprofile != null){
sRGBprofile.close();
}
}

有什么建议可以解决吗?我忘记了什么吗?我已经尝试过使用 java ZipOutputStream,但它有任何不同。


感谢您的回答!我了解 ByteOutputStream 的问题,但我不确定在我的案例中什么是最好的方法。这是一个 Web 应用程序,我需要以某种方式将 zip 打包到数据库 blob 中。

我现在正在做的是使用 iText 将 PDF 直接创建到 ZipOutputStream 并将相应的 ByteArrayOutputSteam 的字节数组保存到 blob。我看到的选项是:

将我的数据拆分为 500 个对象包,将前 500 个 PDF 保存到数据库,然后打开 zip 并添加接下来的 500 个,依此类推......但我认为这会让我遇到与现在相同的情况,即内存中打开的流太大。

尝试将 PDF 保存在服务器上(不确定是否有足够的空间),创建临时 zip 文件,然后将字节提交到 blob...

有什么建议/想法吗?

最佳答案

这是因为您的 ZipOutputStream 由 ByteArrayOutputStream 支持,所以即使关闭条目也会将完整的 ZIP 内容保留在内存中。

关于java - iText - OutOfMemory 创建超过 1000 个 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32891931/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com