PDFBox Rendering Jpeg2000 and JBIG2

1.8k Views Asked by At

I want to convert each pdf page into an image (like a screen shot), and then upload that image to a storage service.

  private void getImageBytes(PDDocument document, int pageIndex, int dpi, ConcurrentHashMap<String, byte[]> imgsToUpload, String imgKey) throws IOException {


    PDFRenderer pdfRenderer = new PDFRenderer(document);
    BufferedImage bim = pdfRenderer.renderImageWithDPI(pageIndex, dpi, ImageType.RGB);
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageIO.write(bim, "png", os); // import javax.imageio.ImageIO;
    os.flush();
    os.close();
    imgsToUpload.put(imgKey, os.toByteArray());
  }

I did import jbig2-imageio by adding this snippet to pom.xml

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.19</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/jbig2-imageio -->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>jbig2-imageio</artifactId>
            <version>3.0.3</version>
        </dependency>

but still the generated images are blank. and this error is logged : Cannot read JPEG2000 image: Java Advanced Imaging (JAI) Image I/O Tools are not installed

So, what did i miss ? , i assumed that adding the dependency would resolve that error.

should i use pdfbox-tools.imageIo instead of java.imageio

PS: I am New to Java, so i it might be a configuration thing ??

1

There are 1 best solutions below

1
On

You need to add these dependencies in your pom file in order to solve the issue.

<dependency>
    <groupId>com.github.jai-imageio</groupId>
    <artifactId>jai-imageio-core</artifactId>
    <version>1.4.0</version>
</dependency>
<dependency>
    <groupId>com.github.jai-imageio</groupId>
    <artifactId>jai-imageio-jpeg2000</artifactId>
    <version>1.4.0</version>
</dependency>

https://pdfbox.apache.org/2.0/dependencies.html#optional-components