0

I have an Java/Gradle application that uses PDFBox to convert PDFs to PNGs. While testing locally on my IDE, my code is as follows:

public static void main(String[] args) throws IOException {
    PDDocument doc = PDDocument.load(new File("pdfFile.pdf"));
    PDFRenderer renderer = new PDFRenderer(doc);
    OutputStream os = new FileOutputStream(new File("image.png"));
    ImageIO.write(renderer.renderImageWithDPI(0, 300), "png", os);
}

In prod, another application launches a new JVM that runs my application. I'm not sure what the classpath for the parent application is, but if I have the following in my Gradle dependencies, does it matter?

implementation "org.apache.pdfbox:jbig2-imageio:3.0.2"

While testing my main method locally on my IDE, it works fine but not using the second setup I described. I have also checked my manifest and can see the following files:

META-INF/services/javax.imageio.spi.ImageReaderSpi
META-INF/services/javax.imageio.spi.ImageWriterSpi 

What am I missing?

user2654096
  • 71
  • 2
  • 8
  • What exact error do you get in the 2nd setup? Do you get an error about a missing jbig2 library or about imageio? Make sure that the jbig2-imageio-*.jar and imageIO is in your classpath. – Lonzak Aug 20 '20 at 07:23
  • I don't have access to the logs as the 2nd setup uses log4j. Do you know how I can configure PDFBox to use the same logger as my application? To your second question, if I have an implementation dependency in my build.gradle does that not mean that the JARs are in my classpath? – user2654096 Sep 15 '20 at 20:09

1 Answers1

1

I reply to your comment as an answer since otherwise formatting/space is limited.

We use slf4j to "redirect" all commons logging to log4j. You have to add the following dependency to your log - then after also adding an appender for pdfbox it will log to your standard logfile...

<!-- PdfBox is using Apache commons logging. To force this to use log4j2 the following lib has to be used -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.7.30</version>
</dependency>

To your 2nd question: No not necessarily. Are all that libs are available on the classpath? Without any error message it is really hard to figure out what exactly is going on. Are you able to see the console? If so then use the following code to check whether your classes are there.

try {
    Class<?> clazz = Class.forName("org.apache.pdfbox.pdmodel.PDDocument");
} 
catch (ClassNotFoundException e) {
    System.err.println("PdfBox classes not found!");
}
Lonzak
  • 9,334
  • 5
  • 57
  • 88