FrenchMyToast
FrenchMyToast

Reputation: 21

PDFBox Out of memory when adding image

I am using PDFBox to extract data from my webapp and put it into a PDF. I have a method that draws the header on each PDF page. However, when I add an image to the each page, the document runs out of memory. I was wondering if anybody had any ideas on a solution? Here is my drawHeader method:

public static void drawHeader(PDDocument doc, PDPage page, PDPageContentStream contentStream, int[] columnWidths, int pageNumber) throws IOException {

    contentStream.beginText();
    PDFont font = PDType1Font.HELVETICA_BOLD;
    contentStream.setFont(font, 24);
    contentStream.moveTextPositionByAmount(50, 750);
    contentStream.drawString("Producer License Report");
    contentStream.endText();

    contentStream.beginText();
    contentStream.moveTextPositionByAmount(550, 750); 

    contentStream.setFont(PDType1Font.HELVETICA_BOLD, 8);
    contentStream.drawString("Page " + pageNumber);
    contentStream.endText();

    contentStream.drawLine(50, 740, 340, 740);
    contentStream.drawLine(16, 680, 595, 680);

    List<String> headerList = new LinkedList<String>();
    headerList.add("NPN");
    headerList.add("First Name");
    headerList.add("Last Name");
    headerList.add("Suffix");
    headerList.add("License State");
    headerList.add("Resident State");
    headerList.add("License Number");

    contentStream.setFont(PDType1Font.HELVETICA_BOLD, 9);
    float textx = 15;
    float texty = 685;

    InputStream in = new FileInputStream(new File("logo.jpg"));
    PDJpeg img = new PDJpeg(doc, in);
    contentStream.drawImage(img, 375, 720);


    for (int i = 0; i < headerList.size(); i++) {
        String text = headerList.get(i);
        contentStream.beginText();
        contentStream.moveTextPositionByAmount(textx, texty);
        contentStream.drawString(text);
        contentStream.endText();
        textx += (columnWidths[i] * 6.5);
    }
}

Upvotes: 2

Views: 2896

Answers (2)

sam9046
sam9046

Reputation: 596

I'm trying to comment the answer by Timo Hoen but don't have enough rep yet...

Another issue I've found with the "out of memory" error is if the image is large, or you have tried to draw it off the page.

Start with your coordinates as 100, 100 and then work from there.

e.g. contentStream.drawImage(img, 100, 100);

Cheers,

Sam

Upvotes: 0

Timo Hoen
Timo Hoen

Reputation: 71

I found a solution! You have to create the Image-Object before opening the contentStream.

Example:

   /* Step 1: Prepare the document.
               */
             doc = new PDDocument();
             PDPage page = new PDPage();
             doc.addPage(page);

             /* Step 2: Prepare the image
              * PDJpeg is the class you use when dealing with jpg images.
              * You will need to mention the jpg file and the document to which it is to be added
              * Note that if you complete these steps after the creating the content stream the PDF
              * file created will show "Out of memory" error.
              */

             PDXObjectImage image = null;
             image = new PDJpeg(doc, new FileInputStream("image.jpg"));
             PDPageContentStream contentStream = new PDPageContentStream(doc,
                    page);
....

Upvotes: 4

Related Questions