Sometimes when you try and convert a very large PDF to image, the following error occurs:
java.lang.OutOfMemoryError: Java heap space
The error message is saying that Java is unable to allocate memory for the image you are trying to create.
The JVM cannot handle a BufferedImage over a certain size. Increasing the heap size does not fix all memory issues in Java (especially if it involves both Java and Native heap).
If you scale the image required down, you will be able to rasterize it. There is sample code here.
The key line is:
For example, reducing size from 1f will reduce if from 100%.
If you get the first page width and height you can check these are sensible figures and scale down if too large. This also allows you to create images of a set height.
There is an example here.