Prevent JVM crash when converting PDF to image

Sometimes when you try and convert a very large PDF to image, the following error occurs:

java.lang.OutOfMemoryError: Java heap space
at java.awt.image.DataBufferInt.<init>(
at java.awt.image.Raster.createPackedRaster(

This means that Java is unable to allocate memory for the image you are trying to create an image of the current PDF page.

The JVM cannot handle a BufferedImage over a certain size (the exact amount is system dependent). Increasing the heap size may help but does not fix all memory issues in Java (especially when they involve both Java and Native heap).

Another solution is to covert the PDF at a smaller scaling. For example, you can create scaled images using ConverPagesToImages as described in the Javadoc examples.

You could also use the getPageDimensions to check the page size before conversion to test if the document is too large for your system to handle then produce the page at a smaller size.