We also used FOP 2.1 and want to achieve, that images inside jars-classpath will be found. Our tested and used solution is the following:
Create your own ResourceResolver
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;
import org.apache.fop.apps.io.ResourceResolverFactory;
import org.apache.xmlgraphics.io.Resource;
import org.apache.xmlgraphics.io.ResourceResolver;
public class ClasspathResolverURIAdapter implements ResourceResolver {
private final ResourceResolver wrapped;
public ClasspathResolverURIAdapter() {
this.wrapped = ResourceResolverFactory.createDefaultResourceResolver();
}
@Override
public Resource getResource(URI uri) throws IOException {
if (uri.getScheme().equals("classpath")) {
URL url = getClass().getClassLoader().getResource(uri.getSchemeSpecificPart());
return new Resource(url.openStream());
} else {
return wrapped.getResource(uri);
}
}
@Override
public OutputStream getOutputStream(URI uri) throws IOException {
return wrapped.getOutputStream(uri);
}
}
- Create the FOPBuilderFactory with your Resolver
FopFactoryBuilder fopBuilder = new FopFactoryBuilder(new File(".").toURI(), new ClasspathResolverURIAdapter());
- Finally address your image
<fo:external-graphic src="classpath:com/mypackage/image.jpg" />
Because you use our own Resolver it is possible to do every lookup which you want.