我正在使用 iText 生成 pdf 格式的非常大的表格。
生成这些表而不是将整个内容存储在内存中的最佳方法是什么?
如果我只是将 for 循环中的以下大小增加到一百万,我就会耗尽内存,是否有比将整个内容都存储在内存中更好的方法来流式传输它
我看到了这个帖子如何以最小的内存占用直接将大内容流式传输为 PDF? https://stackoverflow.com/questions/51491522/how-to-directly-stream-large-content-to-pdf-with-minimal-memory-footprint但我想知道使用什么 iText api。
示例代码:
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class SimpleTable11 {
public final static String DEST = "/Users/.../Documents/test23.pdf";
public static void main(String[] args) throws IOException, DocumentException {
new SimpleTable11().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
System.out.println(new Date());
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
PdfPTable table = new PdfPTable(23);
table.setWidths(new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 });
table.setWidthPercentage(100);
table.addCell(createCell("Account", 2, 1, Element.ALIGN_JUSTIFIED));
table.addCell(createCell("Org Id", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("Contract Number", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("Transaction Type", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("Transaction Number", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("Transaction Date", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("Start Date", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("End Date", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("Billing Reference", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("Line Description", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("Product Name", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("Related Invoices", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("Monthly Unit Price", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("quantity", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("Total Line Tax", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("Total Price", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("Exchange Rate", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("Taxable", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("Vat Rate", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("VAT", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("Taxable", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("VAT", 2, 1, Element.ALIGN_LEFT));
table.addCell(createCell("Total Price", 2, 1, Element.ALIGN_LEFT));
String[] data = { "44445555", "123456", "0105567", "INV", "123456", "10/10/2018", "11/15/2018", "11/20/2050",
"SO-0000000000-Mento", "Marketing Product", "Marketing Product ", "Marketing Product",
"Marketing Product", "0.00", "12.56", "300.00", "0.566667345", "12.54", "10.00%", "12.56", "7.58",
"7.27", "176.67" };
for (int i = 0; i < 20000; i++) {
for (int j = 0; j < data.length; j++) {
table.addCell(createCell(data[j], 1, 1, Element.ALIGN_LEFT));
}
}
document.add(table);
document.close();
System.out.println(new Date());
}
public PdfPCell createCell(String content, float borderWidth, int colspan, int alignment) {
Font font = new Font(FontFamily.HELVETICA, 4, Font.NORMAL);
PdfPCell cell = new PdfPCell(new Phrase(content, font));
cell.setBorderWidth(borderWidth);
cell.setColspan(colspan);
cell.setHorizontalAlignment(alignment);
return cell;
}
}