Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load Fop configuration file to FopFactory

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);
like image 725
guesswho Avatar asked Nov 03 '16 11:11

guesswho


People also ask

What is FOP config?

The FOP configuration file is an XML file containing a variety of settings that are useful for controlling FOP's behavior, and for helping it find resources that you wish it to use. The easiest way to get started using a FOP configuration file is to copy the sample found at {fop-dir}/conf/fop.

What is FOP factory?

Factory class which instantiates new Fop and FOUserAgent instances. This class also holds environmental information and configuration used by FOP. Information that may potentially be different for each rendering run can be found and managed in the FOUserAgent. Constructor Summary. protected.

What is font triplet?

The triplet refers to the unique combination of name, weight, and style for each variation of the font.


1 Answers

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

like image 88
riskop Avatar answered Oct 11 '22 12:10

riskop