英文:
Itext PDF works slowly with Paragraph
问题
我有一个任务,需要创建一个包含最多10000条记录的PDF文件。每条记录都是以JSON表示的Java对象。10个对象约为15000字节(jsonString.length()
)。包含1000条记录的结果文件在磁盘上占用约900 KB。然后我执行以下操作:
private static Font font = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL);
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
Paragraph p = new Paragraph();
p.add(new Paragraph("Title", font));
String jsonString = "{....}"; // 10个对象; jsonString.length()==15000
for (int i = 0; i < 100; i++) // 添加1000个对象
p.add(new Paragraph(jsonString, font));
document.add(p);
document.close();
但是当我运行这段代码时,我需要等待80秒。为什么这么慢?有没有可能更快地完成?
更新
当我使用单个只包含200个对象的段落时,速度甚至更慢:
Paragraph p = new Paragraph("{......}"); // ~300,000字节
document.add(p);
将200个对象保存为PDF需要2分钟,而不是第一种情况中1000个对象所需的1.5分钟。
英文:
I have a task to create a PDF file with maximum of 10000 records. Each record is a Java object in JSON representation. 10 objects are ~15000 bytes (jsonString.length()
). The resulting file with 1000 records takes ~900 KB on disk. I then do:
private static Font font = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL);
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
Paragraph p = new Paragraph();
p.add(new Paragraph("Title", font));
String jsonString = "{....}"; // 10 objects; jsonString.length()==15000
for (int i = 0; i < 100; i++) // add 1000 objects
p.add(new Paragraph(jsonString, font));
document.add(p);
document.close();
But when I run this code I'm waiting 80s. Why it so slow? Is it possible to do it faster?
UPDATE
When I used single Paragraph with only 200 objects it worked even slower:
Paragraph p = new Paragraph("{......}"); // ~300_000 bytes
document.add(p);
It takes 2 mins to save 200 objects as PDF instead of 1.5 min for 1000 objects as in first case.
答案1
得分: 1
我找到了一个解决方案,但是使用OpenPdf库。我创建了一个包含10000条记录的PDF文件,从7到17秒不等,PDF文件大小从4到32MB不等,具体取决于单个记录的大小。
public static void main(String[] args) throws FileNotFoundException {
Document doc = new Document();
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("OpenPDF-example.pdf"));
//设置字体族和颜色
Font font = new Font(Font.HELVETICA, 10);
doc.open();
for (int j = 0; j < 10000; j++) {
Paragraph para = new Paragraph("单个记录从1到5kB", font);
doc.add(para);
}
doc.close();
writer.close();
}
请注意,需要通过小段落添加数据。当我尝试将所有数据添加到一个段落中时,速度非常慢(超过10分钟)。
英文:
I found a solution but with OpenPdf library. I created pdf file with 10000 records from 7 to 17 seconds and pdf size from 4 to 32 MB, depending on single record size.
public static void main(String[] args) throws FileNotFoundException {
Document doc = new Document();
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("OpenPDF-example.pdf"));
//setting font family, color
Font font = new Font(Font.HELVETICA, 10);
doc.open();
for (int j=0; j<10000; j++) {
Paragraph para = new Paragraph("single record from 1 to 5kB", font);
doc.add(para);
}
doc.close();
writer.close();
}
Note, it is required to add data by small paragraphs. When I tried to add all data in one paragraphs it worked very slowly (more than 10 min).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论