簡體   English   中英

iText - OutOfMemory創建超過1000個PDF

[英]iText - OutOfMemory creating more than 1000 PDFs

我想創建一個填充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();
} ...

PdfBuilder

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內容保留在內存中。

你需要使用另一種方法來處理這個數量的參數(1000多個文件)。

您正在將示例中的所有PDF文件加載到內存中,您需要在文檔塊中執行此操作,以最大限度地減少此“內存負載”的影響。

另一種方法是在文件系統上序列化PDF,然后創建zip文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM