cscan cscan - 6 months ago 144
Java Question

Specify a configuration file with FopFactoryBuilder

Originally I was creating my FopFactory like so:

FopFactory.newInstance(new File("config/pdf/fop.xconf"))


Which picked up all of the custom configuration in my configuration file. However, my requirements have changed and I need to use a FopFactoryBuilder to build my FopFactory (which does not take a configuration file as a parameter).

FopFactoryBuilder builder = new FopFactoryBuilder(URI.create("/"), uriResolverAdapter);
Configuration configuration = new DefaultConfiguration("config/pdf/fop.xconf");
builder.setConfiguration(configuration);


But, this doesn't seem to work. I specifically need to include some font related stuff:

<fonts>
<auto-detect/>
<font-base>./</font-base>
<directory recursive="true">config/pdf/fonts</directory>
</fonts>


Edit:

I've also tried this,

DefaultConfigurationBuilder defaultConfigurationBuilder = new DefaultConfigurationBuilder();
builder.setConfiguration(defaultConfigurationBuilder.build("config/pdf/fop.xconf"));


as well as this,

builder.setConfiguration(defaultConfigurationBuilder.buildFromFile(new File("config/pdf/fop.xconf")));


this,

FileInputStream config = new FileInputStream(new File("config/pdf/fop.xconf"));
builder.setConfiguration(defaultConfigurationBuilder.build(config));


this,

builder.setConfiguration(defaultConfigurationBuilder.buildFromFile("config/pdf/fop.xconf"));


and finally this (Jars.getJarFile just gets the file of the running jar):

File jarFile = Jars.getJarFile(this);
if (jarFile!= null) {
String absoluteLocation = jarFile.getParent() + "/config/pdf/fop.xconf";
builder.setConfiguration(defaultConfigurationBuilder.buildFromFile(absoluteLocation));
}


but those all give me an exception when I create a new fop with FopFactory:

java.lang.IllegalArgumentException: URI is not absolute

Answer

What I ended up doing was creating a configuration like this:

DefaultConfigurationBuilder defaultConfigurationBuilder = new DefaultConfigurationBuilder();
builder.setConfiguration(defaultConfigurationBuilder.build("config/pdf/fop.xconf"));

and removing the following line from my configuration:

<auto-detect/>
Comments