You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "serge zr (JIRA)" <ji...@apache.org> on 2014/10/07 11:37:33 UTC

[jira] [Created] (PDFBOX-2404) OutOfMemory or/and IndexOutOfBounds while creating a pdf with lots of images

serge zr created PDFBOX-2404:
--------------------------------

             Summary: OutOfMemory or/and IndexOutOfBounds while creating a pdf with lots of images
                 Key: PDFBOX-2404
                 URL: https://issues.apache.org/jira/browse/PDFBOX-2404
             Project: PDFBox
          Issue Type: Bug
    Affects Versions: 1.8.7
         Environment: Windows 7/ Eclipse
            Reporter: serge zr


Hi Folks,
I try to create PDFs with full page images (as they come from a scanner).
First I got OutOfMemoryError(exception one) after second page (which is already not nice).  
I gave the process more memory (1G) and got the second Exception.
The code breaks around 17 pages with the second exception (IndexOutOfBounds). 
The image in this example was taken from the internet as I can not provide the original, but produces the same issue. 
Another remark - the production is quite slow...

//---------------Start Code----------
package test;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;

public class PDFCreatorTest {
	public static void main(String[] args) {

		try {
			createPDF("new.pdf");
		} catch (COSVisitorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static void createPDF(String pdffile) throws IOException,
			COSVisitorException {

		PDDocument doc = null;
		try {
			doc = new PDDocument();

			int tstsize = 30;
			for (int pid = 0; pid < tstsize; pid++) {

				System.out.println("Create page: " + pid + " of " + tstsize
						+ "...");
				PDPage page = new PDPage();
				doc.addPage(page);

				BufferedImage tmp_image = ImageIO
						.read(new URL(
								"http://carolhagen.files.wordpress.com/2013/06/skewed-demo_wilhomebw-page-001.jpg"));
				PDRectangle box = // page.getCropBox();
				page.getMediaBox();
				BufferedImage image = new BufferedImage(tmp_image.getWidth(),
						tmp_image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
				image.createGraphics().drawRenderedImage(tmp_image, null);

				PDXObjectImage ximage = new PDPixelMap(doc, image);

				PDPageContentStream contentStream = new PDPageContentStream(
						doc, page, true, true);

				float scale = box.getHeight() / ximage.getHeight();
				contentStream.drawXObject(ximage, 0, 0, ximage.getWidth()
						* scale, ximage.getHeight() * scale);

				contentStream.close();
				System.out.println("Page: " + pid + " created!");

				
			}
			System.out.println("Save document" + pdffile);
			doc.save(pdffile);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (doc != null) {
				doc.close();
			}

		}
	}
}

//---------------End Code-------------

//--------------------------- Exception one
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.awt.image.DataBufferByte.<init>(Unknown Source)
	at java.awt.image.Raster.createInterleavedRaster(Unknown Source)
	at java.awt.image.BufferedImage.<init>(Unknown Source)
	at org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap.createImageStream(PDPixelMap.java:130)
	at org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap.<init>(PDPixelMap.java:87)
	at test.PDFCreatorTest.createPDF(PDFCreatorTest.java:65)
	at test.PDFCreatorTest.main(PDFCreatorTest.java:34)

//-------------Exception two----------
java.lang.IndexOutOfBoundsException: Index: 141, Size: 141
	at java.util.ArrayList.rangeCheck(Unknown Source)
	at java.util.ArrayList.get(Unknown Source)
	at org.apache.pdfbox.io.RandomAccessBuffer.seek(RandomAccessBuffer.java:110)
	at org.apache.pdfbox.io.RandomAccessFileOutputStream.write(RandomAccessFileOutputStream.java:106)
	at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
	at java.io.BufferedOutputStream.flush(Unknown Source)
	at java.io.FilterOutputStream.close(Unknown Source)
	at org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap.createImageStream(PDPixelMap.java:233)
	at org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap.<init>(PDPixelMap.java:102)
	at org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap.createImageStream(PDPixelMap.java:222)
	at org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap.<init>(PDPixelMap.java:87)
	at test.PDFCreatorTest.createPDF(PDFCreatorTest.java:56)
	at test.PDFCreatorTest.main(PDFCreatorTest.java:21)



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)