How to load a Fop configuration file into FopFactory

I am creating a PDF file from xml and xsl-fo using Fop-2.1 and spent a lot of time setting up the fop configuration file (I use Cyrillic fonts), according to https://xmlgraphics.apache.org/fop/2.1/configuration. html

checked it on the command line, it works fine:

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

Next, I need to do the same in a java application. The application is a multi-module Maven project.

I was stuck when I tried to get an FopFactory instance with my configuration file located in the service module resources folder, here is the project tree

service module

Src / core / resources

conf / config.xml

web module

How to create an 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 project we use an EJB container and of course

The bean enterprise should not use the java.io package to access files and directories on the file system.

then I'm trying to do it

  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 creation application located in the service module. What do I need to install in baseURI?

 fopFactoryBuilder = new FopFactoryBuilder(baseURI).setConfiguration(cfg); 
+5
source share
1 answer

I ran into a similar problem and created a working demo that

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

Here he is:

https://github.com/riskop/fop_test

See also:

Set baseURI FopFactoryBuilder database to jar classpath

+1
source

Source: https://habr.com/ru/post/1259209/


All Articles