使用iText创建10000多页PDF



我需要在iText(2.1.7)中生成非常长的PDF,其中页面由Graphics2D绘制。问题是,每个页面需要大约2MB,所以创建一个10000页的pdf需要大约20GB的RAM。是否有可能在HDD上每100页刷新一部分com.lowagie.Text.Document?我尝试过PdfWriter.flush(),但没有帮助。

这是一个小代码。用-Xmx200m运行它得到OutOfMemoryError

import java.awt.Color;
import java.awt.Graphics2D;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

public class Main {
    static String FILENAME = "/home/username/tmp/out.pdf";
    public static void main(String[] args) {
        try {
            System.out.println(printMem());
            Rectangle rectPage = new Rectangle(0, 0, 210, 297);
            FileOutputStream fos = new FileOutputStream(new File(FILENAME));
            Document d = new Document(rectPage, 0, 0, 0, 0);
            PdfWriter writer = PdfWriter.getInstance(d, fos);

            d.open();


            int pageNo = 200;
            for (int i = 0; i < pageNo; i++) {
                d.newPage();
                PdfContentByte cb;
                PdfTemplate tp;
                Graphics2D g2;

                cb = writer.getDirectContent();
                tp = cb.createTemplate(rectPage.getWidth(),
                        rectPage.getHeight());
                g2 = tp.createGraphicsShapes(rectPage.getWidth(),
                        rectPage.getHeight());
                paintRand(g2);
                g2.dispose();
                cb.addTemplate(tp, 0, 0);
                System.out.println(i + ") " + printMem());
                writer.flush();

            }

            d.close();
            System.out.println(printMem());
            System.out.println("done");



        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        } catch (OutOfMemoryError mem) {
            System.err.println("OUT OF MEMORY!!!!!!! " + printMem());
            mem.printStackTrace();
        }
    }



    private static String printMem() {
        final long usedMem = Runtime.getRuntime().totalMemory()
                - Runtime.getRuntime().freeMemory();
        String ram = "RAM: " + (usedMem / 1024 / 1024) + "MB / "
                + (Runtime.getRuntime().totalMemory() / 1024 / 1024) + "MB";
        if (Runtime.getRuntime().maxMemory() != Long.MAX_VALUE) {
            ram += " (max limit: " + Runtime.getRuntime().maxMemory() / 1024
                    / 1024 + "MB)";
        } else {
            ram += " (no max limit)";
        }
        return ram;
    }
    private static Random rand = new Random();
    private static List<Color> colors = new ArrayList<Color>()
    {
        {
            add(new Color(33, 33, 33));
            add(new Color(33, 33, 36));
            add(new Color(33, 33, 44));
            add(new Color(33, 33, 38));
        }
    };
    private static void paintRand(Graphics2D g2) {
        int count = 10000;
        for (int i = 0; i < count; i++) {
            g2.setColor(colors.get(rand.nextInt(colors.size())));
            g2.fillOval(rand.nextInt(210), rand.nextInt(210), rand.nextInt(55), rand.nextInt(55));
        }
    }
}

所以我得到

117) RAM: 239MB / 271MB (max limit: 271MB)
118) RAM: 246MB / 271MB (max limit: 271MB)
119) RAM: 244MB / 271MB (max limit: 271MB)
120) RAM: 245MB / 271MB (max limit: 271MB)
121) RAM: 247MB / 271MB (max limit: 271MB)
OUT OF MEMORY!!!!!!! RAM: 242MB / 271MB (max limit: 271MB)
java.lang.OutOfMemoryError: Java heap space
at com.lowagie.text.pdf.ByteBuffer.append_i(Unknown Source)
at com.lowagie.text.pdf.ByteBuffer.append(Unknown Source)
at com.lowagie.text.pdf.ByteBuffer.formatDouble(Unknown Source)
at com.lowagie.text.pdf.ByteBuffer.append(Unknown Source)
at com.lowagie.text.pdf.ByteBuffer.append(Unknown Source)
at com.lowagie.text.pdf.PdfContentByte.HelperRGB(Unknown Source)
at com.lowagie.text.pdf.PdfContentByte.setRGBColorFill(Unknown Source)
at com.lowagie.text.pdf.PdfContentByte.setColorFill(Unknown Source)
at com.lowagie.text.pdf.PdfGraphics2D.setPaint(Unknown Source)
at com.lowagie.text.pdf.PdfGraphics2D.setFillPaint(Unknown Source)
at com.lowagie.text.pdf.PdfGraphics2D.followPath(Unknown Source)
at com.lowagie.text.pdf.PdfGraphics2D.fill(Unknown Source)
at com.lowagie.text.pdf.PdfGraphics2D.fillOval(Unknown Source)
at Main.paintRand(Main.java:121)
at Main.main(Main.java:54)

有没有像BufferedWriter那样,在平静中编写文档的选项?

很难理解为什么仍然使用iText 2.1.7。问题的答案https://stackoverflow.com/questions/25696851/can-itext-2-1-7-or-earlier-can-be-used-commercially是"不!"所以请成为一个运动和升级。

至于你的问题:你应该区分不同的问题。

请检查以下问题的答案:使用带有图像的c#代码生成pdf文件时,pdf文件的大小限制是多少?

您会注意到iText 2.1.7不能用于创建大小大于2GB的文件。知道每个页面需要2MB,并且您希望有10K页面,您应该意识到您不能使用iText 2.1.7来实现您的目标。

你甚至试图制作比PDF 1.4固有的限制更大的文件,所以你必须确保使用压缩的交叉引用表,这是PDF 1.5中引入的功能。

此外:如果您已经阅读了文档,您应该意识到,一旦页面准备就绪,iText就会将每个页面的内容刷新到OutputStream。因此,您的问题"是否有可能在HDD上每100页刷新Document的一部分?"很奇怪。

您对问题的诊断是错误的:您正在创建大量的PdfTemplate对象,并将它们保存在内存中!您应该使用releaseTemplate()方法来编写保存在PdfWriter内存中的所有tp实例。这将释放大量内存。

但在你这么做之前:请做正确的事情并升级!

最新更新