gpt4 book ai didi

java - 下载 PDF 文件时从 itext 获取 Java 堆空间错误

转载 作者:行者123 更新时间:2023-12-01 10:02:39 27 4
gpt4 key购买 nike

当我们尝试导出为 PDF 时,我们遇到了 Java 堆空间错误。下面是堆栈跟踪错误,

SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.OutOfMemoryError: Java heap space
at java.util.HashMap.<init>(HashMap.java:203)
at com.lowagie.text.pdf.PdfChunk.<init>(Unknown Source)
at com.lowagie.text.pdf.PdfChunk.split(Unknown Source)
at com.lowagie.text.pdf.PdfLine.add(Unknown Source)
at com.lowagie.text.pdf.PdfCell.<init>(Unknown Source)
at com.lowagie.text.pdf.PdfTable.updateRowAdditionsInternal(Unknown Source)
at com.lowagie.text.pdf.PdfTable.<init>(Unknown Source)
at com.lowagie.text.pdf.PdfDocument.addPdfTable(Unknown Source)
at com.lowagie.text.pdf.PdfDocument.add(Unknown Source)
at com.lowagie.text.Document.add(Unknown Source)

We are using itext-1.4.4.jar to Export PDF file. There are 30 columns and around 20000 rows. Also we have tried with com.lowagie.text-2.1.7.jar, there also same issue.

Below find the configuration details,

RAM : 8GB
Heap Size : -Xms256m -Xmx1024m -XX:PermSize=64m -XX:MaxPermSize=512m

下面找到将返回字节数组的示例代码片段。将表添加到文档时,我们从 itext pdf 中收到 Java 堆空间错误。

**Sample Code snippet:**

public static byte[] convertToPdf(ResultSet argResultSet,String argReportName) throws Exception {


ResultSetMetaData metaData = null;
ByteArrayOutputStream outputStream = null;
Document document = null;
byte[] byteArray = null;
try {
metaData = argResultSet.getMetaData();
int columnCount = metaData.getColumnCount();

outputStream = new ByteArrayOutputStream();
document = new Document(PageSize.A4, 25, 25, 25, 25);
PdfWriter.getInstance(document, outputStream);
document.open();
Font reportNameFont = FontFactory.getFont(FontFactory.HELVETICA, 14, Font.BOLD, Color.BLUE);
document.add(new Paragraph("Report Name: " + argReportName, reportNameFont));
// add a line break
document.add(new Paragraph(""));

Table table = new Table(columnCount);
table.setWidth(100);
table.setPadding(2);
table.setSpacing(0);
table.setCellsFitPage(true);
table.setBorder(Rectangle.NO_BORDER);

String columnNameHeader = null;
Chunk chunkHeader = null;
Cell cellHeader = null;
float[] widths = new float[columnCount];
//Preparing the column headers
for (int i = 0; i < columnCount; i++) {
columnNameHeader = metaData.getColumnLabel(i + 1);
if(CsdcUtility.isBlank(columnNameHeader, true)) {
columnNameHeader = "NoColumnName"+ i;
}
widths[i] = columnNameHeader.length();
chunkHeader = new Chunk(columnNameHeader, new Font(Font.HELVETICA, 10, Font.BOLD));
cellHeader = new Cell(chunkHeader);
cellHeader.setHorizontalAlignment(Element.ALIGN_CENTER);
cellHeader.setHeader(true);
table.addCell(cellHeader);
}
table.endHeaders();

int rowCount = 0;
int columnType;
String columnValue = "";
Chunk chunk = null;
Cell cell = null;
Log.log("Preparing the column values");
while (argResultSet.next()) {
rowCount++;
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
columnType = metaData.getColumnType(columnIndex + 1);
columnValue = "";
if(columnType == Types.BLOB || columnType == Types.CLOB || columnType == Types.LONGVARBINARY || columnType == Types.BINARY || columnType == Types.VARBINARY) {
columnValue = new String(CSDCEncodeDecodeUtility.base64Encode(ExportExcel.getAsByteArray(argResultSet.getBlob(columnIndex + 1))));
} else {
columnValue = argResultSet.getString(columnIndex + 1);
}
if(CsdcUtility.isBlank(columnValue, true)) {
columnValue = "";
}
if (widths[columnIndex] < columnValue.length()) {
widths[columnIndex] = columnValue.length();
}
chunk = new Chunk(columnValue, new Font(Font.HELVETICA, 10));
cell = new Cell(chunk);
cell.setVerticalAlignment(Element.ALIGN_TOP);
cell.setLeading(8);
table.addCell(cell);
}
}
//Table Prepared - Adding it to Document
document.add(table); // **while adding the table to the document, throwing java heap space error**
document.close();
byteArray = outputStream.toByteArray();
outputStream = null;
} catch(Exception e) {
System.out.println(e.getStackTrace());
}
finally {
if(document.isOpen()) {
document.close();
}
try{
if(argResultSet!=null){
argResultSet.close();
argResultSet=null;
}
if (outputStream != null)
{
outputStream.close();
outputStream = null;
}
}catch (Exception e) {
System.out.println(e.getStackTrace());
}
}
return byteArray;
}

最佳答案

您应该直接写入 HttpServletResponse 的输出流,而不是尝试在内存中创建字节数组。

关于java - 下载 PDF 文件时从 itext 获取 Java 堆空间错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36686965/

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