Itext PDF在处理段落时速度较慢。

huangapple go评论73阅读模式
英文:

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(&quot;Title&quot;, font));

String jsonString = &quot;{....}&quot;; // 10 objects; jsonString.length()==15000
for (int i = 0; i &lt; 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(&quot;{......}&quot;); // ~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(&quot;OpenPDF-example.pdf&quot;));
	//setting font family, color
	Font font = new Font(Font.HELVETICA, 10);
	doc.open();
	for (int j=0; j&lt;10000; j++) {
		Paragraph para = new Paragraph(&quot;single record from 1 to 5kB&quot;, 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).

huangapple
  • 本文由 发表于 2020年8月12日 23:10:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63379494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定