Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDFBox Error When Converting to BufferedImage: NoClassDefFoundError: org/apache/fontbox/FontBoxFont

I'm trying to convert a PDF doc to .png files using PDFBox. I followed this answer to get an idea of what dependencies were needed and to give me a starting point. When I try to loop through the pages and create the BufferedImage I receive a NoClassDefFoundError error. It's looking for org/apache/fontbox/FontBoxFont but through some extensive Googling I've not found a thing about FontBoxFont. Is this a separate jar that needs to be included? What is causing this error? The following .jar's are included in the project:

pdfbox-2.0.2.jar
levigo-jbig2-imageio-1.6.5.jar
pdfbox-tools-2.0.2.jar
jai-imageio-core-1.3.1.jar
commons-logging-1.2.jar

Here is the main method:

public static void main(String[] args) {

    String sourceDir = "C:/Dev/Workspace/PdfToPng/Stocks.pdf";
    String destinationDir = "C:/Dev/Workspace/PdfToPng/pages/";

    try {
        PDDocument document = PDDocument.load(new File(sourceDir));
        PDFRenderer pdfRenderer = new PDFRenderer(document);
        for(int page = 0; page < document.getNumberOfPages(); ++page) {
            BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
            ImageIOUtil.writeImage(bim, destinationDir + (page+1) + ".png", 300);
        }
        document.close();
    } catch(Exception e) {
        System.out.println(e.getStackTrace());
    }
}

The error is thrown on BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/fontbox/FontBoxFont
    at org.apache.pdfbox.pdmodel.font.PDFontFactory.createFont(PDFontFactory.java:75)
    at org.apache.pdfbox.pdmodel.PDResources.getFont(PDResources.java:123)
    at org.apache.pdfbox.contentstream.operator.text.SetFontAndSize.process(SetFontAndSize.java:60)
    at org.apache.pdfbox.contentstream.PDFStreamEngine.processOperator(PDFStreamEngine.java:815)
    at org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:472)
    at org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:446)
    at org.apache.pdfbox.contentstream.PDFStreamEngine.processPage(PDFStreamEngine.java:149)
    at org.apache.pdfbox.rendering.PageDrawer.drawPage(PageDrawer.java:189)
    at org.apache.pdfbox.rendering.PDFRenderer.renderPage(PDFRenderer.java:208)
    at org.apache.pdfbox.rendering.PDFRenderer.renderImage(PDFRenderer.java:139)
    at org.apache.pdfbox.rendering.PDFRenderer.renderImageWithDPI(PDFRenderer.java:94)
    at PdfToPng.main(PdfToPng.java:25)
Caused by: java.lang.ClassNotFoundException: org.apache.fontbox.FontBoxFont
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 12 more
like image 776
Shaggy Avatar asked Sep 13 '16 19:09

Shaggy


1 Answers

Obviously you need to add Apache FontBox 2.0.2 to your classpath, you can get it from mvnrepository

like image 57
Nicolas Filotto Avatar answered Nov 17 '22 17:11

Nicolas Filotto