我正在尝试解析html模板并使用itex库将其转换为pdf。它在staging和qa中正常工作但在生产中丢失了内存错误.Below是示例代码。
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);
下面是错误的堆栈跟踪 - :
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。任何人都可以帮忙
答案 0 :(得分:0)
问题是我在我的html模板中使用了400KB大小的图像,并且生产机器的内存非常少。因此,在解析html文件时,它会丢失内存错误。
答案 1 :(得分:-1)
outputStream = new FileOutputStream(filePath); 这里: - 你的代码中没有关闭outputStream。 请检查一次或尝试使用资源。
https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html