【问题标题】:out of memory heap space error while generating pdf through itext通过itext生成pdf时内存堆空间不足错误
【发布时间】:2016-11-17 07:51:21
【问题描述】:

我正在尝试解析 html 模板并使用 itex 库将其转换为 pdf。它在 staging 和 qa 中工作正常,但在生产中抛出内存错误。下面是示例代码。

 String template = null;
  Document document = null;
  HTMLWorker htmlWorker;
  OutputStream outputStream = null;
  document = new Document();
  document.open();
  document.addTitle("SeatSeller Agent Invoice");
  document.addHeader("Content-Disposition", "attachment");
  document.addHeader("name", "redbusTicket");
  document.addHeader("filename", "test.pdf");
  document.addHeader("Content-Type", "application/pdf");
  document.addHeader("Content-ID", "test.pdf");
  document.addHeader("Content-Description", "SeatSellerInvoice.pdf");
  htmlWorker = new HTMLWorker(document);
  Handlebars handlebars = new Handlebars();
  String filePath = null;

    template = handlebars.compile("invoicepdftemplate").apply(creditInvoice);
    filePath = creditInvoice.getAccount().toString() + "_"
        + MMM_FORMATTER.format(creditInvoice.getDate())
        + YYYY_FORMATTER.format(creditInvoice.getDate()) + ".pdf";


  htmlWorker.parse(new StringReader(template));

  document.close();

  outputStream = new FileOutputStream(filePath);

  ITextRenderer iTextRenderer = new ITextRenderer();
  iTextRenderer.setDocumentFromString(template);
  iTextRenderer.layout();
  iTextRenderer.createPDF(outputStream);
  File fileToBeUploaded = new File(filePath);

下面是error-的stacktrace:

java.lang.OutOfMemoryError: Java heap space
    at com.lowagie.text.pdf.codec.PngImage.decodeIdat(Unknown Source) ~[itext-2.1.7.jar:na]
    at com.lowagie.text.pdf.codec.PngImage.getImage(Unknown Source) ~[itext-2.1.7.jar:na]
    at com.lowagie.text.pdf.codec.PngImage.getImage(Unknown Source) ~[itext-2.1.7.jar:na]
    at com.lowagie.text.pdf.codec.PngImage.getImage(Unknown Source) ~[itext-2.1.7.jar:na]
    at com.lowagie.text.Image.getInstance(Unknown Source) ~[itext-2.1.7.jar:na]
    at com.lowagie.text.Image.getInstance(Unknown Source) ~[itext-2.1.7.jar:na]
    at com.lowagie.text.html.simpleparser.HTMLWorker.startElement(Unknown Source) ~[itext-2.1.7.jar:na]
    at com.lowagie.text.xml.simpleparser.SimpleXMLParser.processTag(Unknown Source) ~[itext-2.1.7.jar:na]
    at com.lowagie.text.xml.simpleparser.SimpleXMLParser.go(Unknown Source) ~[itext-2.1.7.jar:na]
    at com.lowagie.text.xml.simpleparser.SimpleXMLParser.parse(Unknown Source) ~[itext-2.1.7.jar:na]
    at com.lowagie.text.html.simpleparser.HTMLWorker.parse(Unknown Source) ~[itext-2.1.7.jar:na]

代码中是否有内存泄漏或任何问题。生产机器内存为 Xms-256 Xmx-365。谁能帮忙

【问题讨论】:

  • 您在哪一行获得了 OOME?共享堆栈跟踪。
  • 除了手头的问题,您正在使用HTMLWorker。这已在 2011 年被 XMLWorker 取代。
  • 请遵循正确的 iText 示例。您创建了一个 iText Document 但没有附加作者,因此您无法在任何地方编写 PDF。然后,您单独使用 ITextRenderer,它不是 iText 的 part,而是其他库使用的 iText 功能的 wrapper。此外考虑更新。 iText 2.1.7 很古老。
  • 365Mb 现在相当低。您是否尝试增加此值?你能配置 JVM 在 OOME 上做 heapdump 吗?这肯定会揭示问题的根本原因。

标签: java html pdf itext


【解决方案1】:

问题是我在我的 html 模板中使用了 400KB 大小的图像,而生产机器的内存非常少。所以在解析 html 文件时它抛出了内存错误。

【讨论】:

    【解决方案2】:

    outputStream = new FileOutputStream(filePath); 此处:- outputStream 未在您的代码中关闭。 请检查一次或使用资源实施尝试。

    https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

    【讨论】:

    • FileOutputStream 不需要消耗堆内存。关闭它只会释放 FD (hg.openjdk.java.net/jdk7/jdk7/jdk/file/9b8c96f96a0f/src/windows/…)
    • @rkosegi 那为什么OutputStream要实现Closeable呢?
    • 因为它需要关闭FD。检查来源顺便说一句。实现 (Auto)Closeable 并不意味着您的对象正在消耗大量堆。它通常是关于关闭资源的合同。
    猜你喜欢
    • 2014-01-04
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    相关资源
    最近更新 更多