How to load Fop configuration file to FopFactory
Asked Answered
O

1

8

I'm generating PDF file from xml and xsl-fo using Fop-2.1 and spent a lot of time to configure fop configuration file(I'm using cyrillic fonts), in according with https://xmlgraphics.apache.org/fop/2.1/configuration.html

tested it within command-line, it works fine:

fop -c conf.xml -xml xml -xsl xsl -pdf pdf

Next i need to do same in java web application. Application is multi-module Maven project.

I got stuck when tried to get FopFactory instance with my configuration file located in resources folder of service module, here is a project tree

service-module

src/main/resources

conf/config.xml

web-module

How to create instance of FopFactory with my configuration file?

First i did this:

 FopFactory fopFactory = fopFactory = FopFactory.newInstance(
            new File("/full/path/../resources/conf/config.xml"));

in this a project we using EJB container, and of course

An enterprise bean must not use the java.io package to attempt to access files and directories in the file system.

then I'm trying this one

    DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
    Configuration cfg = null;
    try {
        cfg = cfgBuilder.build(getClass().getClassLoader().getResourceAsStream("conf/config.xml"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    FopFactoryBuilder factoryBuilder = new FopFactoryBuilder(URI.create("/")).setConfiguration(cfg);
    FopFactory fopFactory = factoryBuilder.build();

PDF generating app located in service module. What should i need to set in baseURI?

fopFactoryBuilder = new FopFactoryBuilder(baseURI).setConfiguration(cfg);
Orchestrate answered 3/11, 2016 at 11:44 Comment(2)
Did you ever find a way to do this correctly?Roily
had to use previous versions of fop 1.**Orchestrate
C
4

I bumped into a similar problem and created a working demo which

  • uses fop 2.2.
  • loads everything from classpath (even a ttf font to embed into the pdf)

Here it is:

https://github.com/riskop/fop_test

See also this:

Set FopFactoryBuilder baseURI to jar classpath

Colombi answered 5/4, 2018 at 12:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.