0

The error I get is:

Image not found.URI:xslfile/ABC.jpg

I wanted to upgrade Apache FOP from 1.1 to 2.8 due to vulnerability issue, but after the upgrade the image is not getting loaded in the pdf file.
I want to insert an Imange in the top left corner of the pdf file. My image is inside src/main/resources/xslfile/ABC.jpg but FOP is giving Image not found error even though the path is correct. The PDF file is getting downloaded with other texts and tables without the image.
Before upgrading i was using Apache FOP 1.1 and everything was working fine.

I have upgraded Apache FOP from 1.1 -> 2.8 and I have an existing PDF created out of XSL-FO and XML, I have added all these to my code as given in the official Apache FOP upgrading page I have also created the ClassPathResolver which looks like this

package pack;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;

import org.apache.fop.apps.io.ResourceResolverFactory;
import org.apache.xmlgraphics.io.Resource;
import org.apache.xmlgraphics.io.ResourceResolver;

/**
 * ResourceResolver which loads resources with "classpath:" prefix from the classpath 
 * 
 * Based on this:
 * 
 * https://stackoverflow.com/questions/41661997/set-fopfactorybuilder-baseuri-to-jar-classpath
 * 
 */
public class ClasspathResolverURIAdapter implements ResourceResolver {

    private final ResourceResolver wrapped;

    public ClasspathResolverURIAdapter() {
      this.wrapped = ResourceResolverFactory.createDefaultResourceResolver();
    }
    
    @Override
    public Resource getResource(URI uri) throws IOException {
        System.out.println("getresource called, uri is: " + uri + ", path is: " + uri.getSchemeSpecificPart());
        if (uri.getScheme().equals("classpath")) {
            InputStream is = Start.class.getResourceAsStream(uri.getSchemeSpecificPart());
            System.out.println("is: " + is);
            return new Resource(is);
        }
        else {
            return wrapped.getResource(uri);
        }
    }

    @Override
    public OutputStream getOutputStream(URI uri) throws IOException {
        if (uri.getScheme().equals("classpath")) {
            throw new RuntimeException("This is a read only ResourceResolver. You can not use it's outputstream");
        }
        else {
            return wrapped.getOutputStream(uri);
        }
    }

}

In my XSL file i have added the image inside <fo:block> as

<fo:external-graphic src="xslfile/ABC.jpg"width="40mm" height="10mm" content-width="scale-to-fit"content-height="scale-to-fit" scaling="non-uniform" />

But when i deploy the code in server it's creating the pdf but it's not loading the image I added. I have also tried loading images with PDFBox similar to the zip file provided in this link fop-pdf-images-2.8-bin.tar.gz Please suggest any solutions if any of you have upgraded apache fop 2.8 with images.

lfurini
  • 3,729
  • 4
  • 30
  • 48

0 Answers0