Animesh Pandey
Animesh Pandey

Reputation: 6018

Converting a PDF to text using Tesseract OCR

AIM: convert a PDF to base64 where PDF can be a general PDF or a scanned one.

I am using Tesseract OCR for converting scanned PDFs to text files. Since I am working in Java, I am using terr4j library for this.

The flow of program as I have thought would be as follows:

Get PDF file ---> Convert each page to image using Ghost4j ---> Pass each image to tess4f for OCR ---> convert whole text to base64.

I have been able to convert a PDF file to Images using following code:

package helpers;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.awt.Image;
import java.awt.image.RenderedImage;
import java.util.List;
import javax.imageio.ImageIO;

import org.ghost4j.document.DocumentException;
import org.ghost4j.document.PDFDocument;
import org.ghost4j.analyzer.FontAnalyzer;
import org.ghost4j.renderer.RendererException;
import org.ghost4j.renderer.SimpleRenderer;
import net.sourceforge.tess4j.*;

class encoder {
    public static byte[] createByteArray(File pCurrentFolder, String pNameOfBinaryFile) {
        String pathToBinaryData = pCurrentFolder.getAbsolutePath()+"/"+pNameOfBinaryFile;

        File file = new File(pathToBinaryData);
        if (!file.exists()) {
            System.out.println(pNameOfBinaryFile+" could not be found in folder "+pCurrentFolder.getName());
            return null;
        }

        FileInputStream fin = null;
        try {
            fin = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        byte fileContent[] = new byte[(int) file.length()];
        try {
            if (fin != null)
                fin.read(fileContent);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileContent;
    }

    public void covertToImage(File pdfDoc) {
        PDFDocument document = new PDFDocument();
        try {
            document.load(pdfDoc);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SimpleRenderer renderer = new SimpleRenderer();
        renderer.setResolution(300);
        List<Image> images = null;
        try {
            images = renderer.render(document);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (RendererException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        try {
            if (images != null) {
                // for testing only 1 page
                ImageIO.write((RenderedImage) images.get(10), "png", new File("/home/cloudera/Downloads/1.png"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public class encodeFile {
    public static void main(String[] args) {
        /* This part is for pure PDF files i.e. not scanned */
        //byte[] arr = encoder.createByteArray(new File("/home/cloudera/Downloads/"), "test.pdf");
        //String result = javax.xml.bind.DatatypeConverter.printBase64Binary(arr);
        //System.out.println(result);

        /* This part create the image for a page of scanned PDF file */
        new encoder().covertToImage(new File("/home/cloudera/Downloads/isl99201.pdf")); // results in 1.png

        /* This part is for OCR */
        Tesseract instance = new Tesseract();
        String res = instance.doOCR(new File("/home/cloudera/Downloads/1.png"));
        System.out.println(res);
    }
}

Running this produces these errors:

This occurs when I try to create an image from the PDF. I have seen that if I remove tess4j from build.sbt, image is created with out any errors but I have to use it with that.

Connected to the target VM, address: '127.0.0.1:46698', transport: 'socket'
Exception in thread "main" java.lang.AbstractMethodError: com.sun.jna.Structure.getFieldOrder()Ljava/util/List;
    at com.sun.jna.Structure.fieldOrder(Structure.java:884)
    at com.sun.jna.Structure.getFields(Structure.java:910)
    at com.sun.jna.Structure.deriveLayout(Structure.java:1058)
    at com.sun.jna.Structure.calculateSize(Structure.java:982)
    at com.sun.jna.Structure.calculateSize(Structure.java:949)
    at com.sun.jna.Structure.allocateMemory(Structure.java:375)
    at com.sun.jna.Structure.<init>(Structure.java:184)
    at com.sun.jna.Structure.<init>(Structure.java:172)
    at com.sun.jna.Structure.<init>(Structure.java:159)
    at com.sun.jna.Structure.<init>(Structure.java:151)
    at org.ghost4j.GhostscriptLibrary$display_callback_s.<init>(GhostscriptLibrary.java:63)
    at org.ghost4j.Ghostscript.buildNativeDisplayCallback(Ghostscript.java:381)
    at org.ghost4j.Ghostscript.initialize(Ghostscript.java:336)
    at org.ghost4j.renderer.SimpleRenderer.run(SimpleRenderer.java:105)
    at org.ghost4j.renderer.AbstractRemoteRenderer.render(AbstractRemoteRenderer.java:86)
    at org.ghost4j.renderer.AbstractRemoteRenderer.render(AbstractRemoteRenderer.java:70)
    at helpers.encoder.covertToImage(encodeFile.java:62)
    at helpers.encodeFile.main(encodeFile.java:86)
Disconnected from the target VM, address: '127.0.0.1:46698', transport: 'socket'

Process finished with exit code 1

This error occurs while passing any image to tess4j:

Connected to the target VM, address: '127.0.0.1:46133', transport: 'socket'
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'tesseract': Native library (linux-x86-64/libtesseract.so) not found in resource path (....)
    at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:271)
    at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:398)
    at com.sun.jna.Library$Handler.<init>(Library.java:147)
    at com.sun.jna.Native.loadLibrary(Native.java:412)
    at com.sun.jna.Native.loadLibrary(Native.java:391)
    at net.sourceforge.tess4j.util.LoadLibs.getTessAPIInstance(LoadLibs.java:78)
    at net.sourceforge.tess4j.TessAPI.<clinit>(TessAPI.java:40)
    at net.sourceforge.tess4j.Tesseract.init(Tesseract.java:360)
    at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:273)
    at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:205)
    at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:189)
    at helpers.encodeFile.main(encodeFile.java:89)
Disconnected from the target VM, address: '127.0.0.1:46133', transport: 'socket'

Process finished with exit code 1

I am working on Intellij using SBT on 64 bit CentOS 6.6. By some internet search I have able to understand the issues above but I am facing two constraints:

Following is the part from build.sbt which handles all the required libraries:

"org.ghost4j" % "ghost4j" % "0.5.1",
"org.bouncycastle" % "bctsp-jdk14" % "1.46",
"net.sourceforge.tess4j" % "tess4j" % "2.0.0",
"com.github.jai-imageio" % "jai-imageio-core" % "1.3.0"
 "net.java.dev.jna" % "jna" % "3.4.0", // does not make any difference as only 4.1.0 is installed.

Please help me out in this problem.

UPDATE: I added "net.java.dev.jna" % "jna" % "3.4.0" force() to build.sbt and it solved my first problem.

Upvotes: 1

Views: 11230

Answers (1)

Animesh Pandey
Animesh Pandey

Reputation: 6018

The solution to this issue lies in the Tesseract-API that I found on github. I forked it into my Github account and added a test for a scanned image and did some code refactoring. This way to library started to function properly. The scanned doc I used for testing is here.

I built it successfully on Travis and now it working fine on 32 as well as 64 bit systems.

Upvotes: 1

Related Questions