PDF Box generating blank images due to JBIG2 Images in it
Asked Answered
T

5

12

Let me give you an overview of my project first. I have a pdf which I need to convert into images(One image for one page) using PDFBox API and write all those images onto a new pdf using PDFBox API itself. Basically, converting a pdf into a pdf, which we refer to as PDF Transcoding.

For certain pdfs, which contain JBIG2 images, PDFbox implementation of convertToImage() method is failing silently without any exceptions or errors and finally, producing a PDF, but this time, just with blank content(white). The message I am getting on the console is:

Dec 06, 2013 5:15:42 PM org.apache.pdfbox.filter.JBIG2Filter decode
SEVERE: Can't find an ImageIO plugin to decode the JBIG2 encoded datastream.
Dec 06, 2013 5:15:42 PM org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap getRGBImage
SEVERE: Something went wrong ... the pixelmap doesn't contain any data.
Dec 06, 2013 5:15:42 PM org.apache.pdfbox.util.operator.pagedrawer.Invoke process
WARNING: getRGBImage returned NULL

I need to know how to resolve this issue? We have something like:

import org.apache.pdfbox.filter.JBIG2Filter; 

which I don't know how to implement.

I am searching on that, but to no avail. Could anyone please suggest?

Toilette answered 6/12, 2013 at 13:12 Comment(3)
Ah, the exception gives a hint: "Can't find an ImageIO plugin to decode the JBIG2 encoded datastream"; PDFBox uses Java standard classes when rendering images, and they require external JBIG2 support.Industrialist
But then what's the use of import org.apache.pdfbox.filter.JBIG2FilterToilette
what's the use of JBIG2Filter - As the Javadoc of that class says it is Modeled on the JBIG2Decode filter. According to the specification ISO 32000-1 section 7.4.7 "JBIG2Decode Filter": The JBIG2Decode filter (PDF 1.4) decodes monochrome (1 bit per pixel) image data that has been encoded using JBIG2 encoding.Industrialist
P
12

Take a look at this ticket in PDFBox https://issues.apache.org/jira/browse/PDFBOX-1067 . I think the answer to your question is:

  1. to make sure that you have JAI and the JAI-ImageIO plugins installed for your version of Java: decent installation instructions are available here: http://docs.geoserver.org/latest/en/user/production/java.html
  2. to use the JBIG2-imageio plugin, (newer versions are licensed under the Apache2 license) https://github.com/levigo/jbig2-imageio/
Publishing answered 10/1, 2014 at 23:20 Comment(1)
I downloaded jbig2-imageio-3.0.0.jar from search.maven.org/remotecontent?filepath=org/apache/pdfbox/… and this solved the problem (thank you!) and it has an Apache license which is more permissive than GPL3 ...Fantom
M
5

I had the same problem and I fixed it by adding this dependency in my pom.xml :

<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>jbig2-imageio</artifactId>
    <version>3.0.2</version>
</dependency>

Good luck.

Montiel answered 9/7, 2019 at 18:45 Comment(0)
P
3

I had the exact same problem. I downloaded the jar from jbig2-imageio and I just included it in my project's application libraries, and it worked right out of the box. As adam said, it uses GPL3.

Pugh answered 11/2, 2015 at 16:21 Comment(0)
E
1

Installing the JAI seems not needed. I only needed to download the levigo-jbig2-imageio-1.6.5.jar, place it in the folder of my dependency-jars and in eclipse add it to the java build path libraries. https://github.com/levigo/jbig2-imageio/

Enharmonic answered 15/8, 2015 at 13:35 Comment(0)
A
1
import java.awt.image.BufferedImage
import org.apache.pdfbox.cos.COSName

import org.apache.pdfbox.pdmodel.PDDocument
import org.apache.pdfbox.pdmodel.PDPage
import org.apache.pdfbox.pdmodel.PDPageTree
import org.apache.pdfbox.pdmodel.PDResources
import org.apache.pdfbox.pdmodel.graphics.PDXObject
import org.apache.pdfbox.rendering.ImageType
import org.apache.pdfbox.rendering.PDFRenderer
import org.apache.pdfbox.tools.imageio.ImageIOUtil


import javax.imageio.ImageIO
import javax.imageio.spi.IIORegistry
import javax.imageio.spi.ImageReaderSpi
import javax.swing.*
import javax.swing.filechooser.FileNameExtensionFilter

public class savePDFAsImage{

    String path = "c:/pdfImage/"

    //allow pdf file selection for extracting
    public static File selectPDF() {
        File file = null
        JFileChooser chooser = new JFileChooser()
        FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF", "pdf")
        chooser.setFileFilter(filter)
        chooser.setMultiSelectionEnabled(false)
        int returnVal = chooser.showOpenDialog(null)
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile()
           println "Please wait..."
        }
        return file
    }

    public static void main(String[] args) {
        try {
 // help to view list of plugin registered. check by adding JBig2 plugin and JAI plugin
            ImageIO.scanForPlugins()
            IIORegistry reg = IIORegistry.getDefaultInstance()
            Iterator spIt = reg.getServiceProviders(ImageReaderSpi.class, false)
            spIt.each(){
                println it.getProperties()
            }
            testPDFBoxSaveAsImage()
            testPDFBoxExtractImagesX()
        } catch (Exception e) {
            e.printStackTrace()
        }
    }    

    public static void testPDFBoxExtractImagesX() throws Exception {
        PDDocument document = PDDocument.load(selectPDF())
        PDPageTree list = document.getPages()
        for (PDPage page : list) {
            PDResources pdResources = page.getResources()
            for (COSName c : pdResources.getXObjectNames()) {
                PDXObject o = pdResources.getXObject(c)
                if (o instanceof org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject) {
                    File file = new File( + System.nanoTime() + ".png")
                    ImageIO.write(((org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject) o).getImage(), "png", file)
                }
            }
        }
        document.close()
        println "Extraction complete"
    }
    public static void testPDFBoxSaveAsImage() throws Exception {
        PDDocument document = PDDocument.load(selectPDF().getBytes())
        PDFRenderer pdfRenderer = new PDFRenderer(document)
        for (int page = 0; page < document.getNumberOfPages(); ++page) {
            BufferedImage bim = pdfRenderer.renderImageWithDPI(page,300, ImageType.BINARY)
            // suffix in filename will be used as the file format
            OutputStream fileOutputStream = new FileOutputStream(+ System.nanoTime() + ".png")
            boolean b = ImageIOUtil.writeImage(bim, "png",fileOutputStream,300)
        }
        document.close()
        println "Extraction complete"
    }
}
Articulator answered 20/3, 2017 at 19:7 Comment(2)
It is always better when you provide some brief explanation about your code snippet. It helps others to understand your code better.Hurlee
In the above code 1 . test for different plugin supported by JVM is covered by ImageIO block. 2 . testPDFBoxSaveAsImage() will save the pages as single image. 3.testPDFBoxExtractImagesX() will extract the images from PDF using the getxObject.Articulator

© 2022 - 2024 — McMap. All rights reserved.