9

I'm upgrading an Apache FOP 1.0 project to Apache FOP 2.1. In this project, all necessary files are packaged within the jar file.

I've added the new FopFactoryBuilder to generate a FopFactory

    FopFactoryBuilder builder = new FopFactoryBuilder(new File(".").toURI());
    builder = builder.setConfiguration(config);
    fopFactory = builder.build();

but all my resouces are loaded from the relative path on my file system, not from the jar. How can I set the baseURI to the jar's classpath?

Thanks

sbo
  • 951
  • 2
  • 12
  • 25

2 Answers2

10

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);
  }

}
  1. Create the FOPBuilderFactory with your Resolver
FopFactoryBuilder fopBuilder = new FopFactoryBuilder(new File(".").toURI(), new ClasspathResolverURIAdapter());
  1. 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.

JaXt0r
  • 781
  • 9
  • 13
  • 1
    I bumped into a similar problem (wanted to load everything from inside the jar, config and even fonts to embed into the pdf), and created a working demo based on JaxtOr's post. Here it is: https://github.com/riskop/fop_test – riskop Apr 05 '18 at 12:18
  • 1
    Please change `(uri.getScheme().equals("classpath"))` to `"classpath".equals(uri.getScheme()))` to avoid NullPointerException for relative URIs – Vytenis Bivainis May 04 '18 at 14:20
  • amazing solution, thank you so much for sharing it. – Marti Pàmies Solà Oct 18 '21 at 10:16
  • Unfortunately this solution does not work since 2.7, fonts that are being loaded from `.xconf` will throw the following: `unknown protocol: classpath: java.net.MalformedURLException: unknown protocol: classpath` – sm3sher Jan 11 '23 at 13:24
2

By specifying the URL as a classpath URL like:

<fo:external-graphic src="classpath:fop/images/myimage.jpg"/>

In this example the file is a resource in the resource-package fop.images but the actual file gets later packed to some entirely different place inside the JAR, which is - however - part of the classpath, so the lookup as above works.

TwoThe
  • 13,879
  • 6
  • 30
  • 54