我正在使用 iText 1.4.1。
我正在尝试将简单表格添加到文档中。
但它在创建 PDF 时抛出了内存不足异常。
该表正在从有十万条记录的数据库中获取数据。
这是我的示例代码。
public String createPDF(List<OutboundPriorityGridDTO> resultList,
ExportInputDTO arg1) {
Document document = new Document(PageSize.A4.rotate(), 15, 15, 25, 25);
try
{
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filePath));
TableHeader includeHeader = new TableHeader();
writer.setPageEvent(includeHeader);
document.open();
document.addCreationDate();
if (isEmptyList(resultList)){
document.add(new Phrase(new Chunk("\n**** No Records are returned. ***", PdfConstants.normalFont12)));
stampAndFlushFilePath(document, filePath, getPageOrientation(document));
return filePath;
}
for (OutboundPriorityGridDTO gridDto : resultList) {
light = zeroIfNull(gridDto.getTotalOlpns())
.subtract(zeroIfNull(gridDto.getHeavyOlpns()));
table.addCell(createPdfCell(cell,gridDto.getShipmentNumber(),Element.ALIGN_LEFT,0,BaseColor.BLACK,fontType));
addInterimColumn( table, fontType);
table.addCell(createPdfCell(cell,gridDto.getCarrier(),Element.ALIGN_LEFT,0,BaseColor.BLACK,fontType));
addInterimColumn( table, fontType);
table.addCell(createPdfCell(cell, gridDto.getDestination(),Element.ALIGN_LEFT,0,BaseColor.BLACK,fontType));
addInterimColumn( table, fontType);
table.addCell(createPdfCell(cell,gridDto.getEstimatedWeight(),Element.ALIGN_RIGHT,0,BaseColor.BLACK,fontType));
addInterimColumn( table, fontType);
table.addCell(createPdfCell(cell,gridDto.getEstimatedCube(),Element.ALIGN_RIGHT,0,BaseColor.BLACK,fontType));
addInterimColumn( table, fontType);
table.addCell(createPdfCell(cell,gridDto.getDateCreated(),Element.ALIGN_LEFT,0,BaseColor.BLACK,fontType));
addInterimColumn( table, fontType);
table.addCell(createPdfCell(cell,gridDto.getStop(),Element.ALIGN_RIGHT,0,BaseColor.BLACK,fontType));
addInterimColumn( table, fontType);
table.addCell(createPdfCell(cell,gridDto.getDockDoorName(),Element.ALIGN_LEFT,0,BaseColor.BLACK,fontType));
addInterimColumn( table, fontType);
table.addCell(createPdfCell(cell,gridDto.getTrailerNumber(),Element.ALIGN_LEFT,0,BaseColor.BLACK,fontType));
addInterimColumn( table, fontType);
table.addCell(createPdfCell(cell,light,Element.ALIGN_RIGHT,0,BaseColor.BLACK,fontType));
addInterimColumn( table, fontType);
table.addCell(createPdfCell(cell,gridDto.getHeavyOlpns(),Element.ALIGN_RIGHT,0,BaseColor.BLACK,fontType));
addInterimColumn( table, fontType);
table.addCell(createPdfCell(cell,gridDto.getTotalOlpns(),Element.ALIGN_RIGHT,0,BaseColor.BLACK,fontType));
addInterimColumn( table, fontType);
table.addCell(createPdfCell(cell,gridDto.getShipmentStatus(),Element.ALIGN_LEFT,0,BaseColor.BLACK,fontType));
table.completeRow();
}
stampAndFlushFilePath(document, filePath, getPageOrientation(document));
public static void stampAndFlushFilePath(Document document, String filePath, boolean isPageRotated) throws IOException, FileNotFoundException, DocumentException {
document.close();
LOGGER.info(filePath+" successfully created.");
PdfReader reader = new PdfReader(new FileInputStream(filePath));
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filePath));
int n = reader.getNumberOfPages();
getFooterTable(n, document, stamper, isPageRotated);
stamper.close();
}
任何人都可以建议我如何在不增加 jvm 堆大小的情况下摆脱这个异常...