Ivan
Ivan

Reputation: 73

Apache FOP cannot find external image by URI in fatJar

I'm trying to generate PDF with Apache FOP 2.2 and got some problem I'm using full URI like
file:///Users/mac/Desktop/image.png.
and here is part of myTemplate.

<fo:block>
      <fo:external-graphic content-height="scale-to-fit"  content-width="46.0mm"scaling="non-uniform"  src="(here I put the upper URI)"/>
</fo:block>`.    
I also got fop_config.xml in resources folder than looks like   
`<fop version="1.0">
    <renderers>
        <renderer mime="application/pdf">
            <auto-detect/>
<fonts>
<---! here are my fonts --->
</fonts>
        </renderer>
    </renderers>
</fop>

and my script

 val fopFactory = FopConfParser(File("fop_config.xml"))
            .fopFactoryBuilder
            .build()
        val foUserAgent = fopFactory.newFOUserAgent()
        val outStream = BufferedOutputStream(FileOutputStream(File("my_pdf.pdf")))
        outStream.use { out ->
            val fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out)
            val transformer = TransformerFactory.newInstance().newTransformer()
            val src = StreamSource(StringReader(myTemplate))
            val res = SAXResult(fop.defaultHandler)
            transformer.setParameter("versionParam", "2.0")
            transformer.transform(src, res)
        }

Im using Kotlin, but it does not matters. I'm rendering fine via IDEA, but when I create fat Jar with shadowJar Gradle plugin I've got
SEVERE: Image not found. URI: file:///Users/mac/Desktop/image.png. (No context info available)

Why I can't use URI inside of fatJar and how I can fix it?

UPD

I discovered that my problem connected with ImagePreloader. exception

org.apache.xmlgraphics.image.loader.ImageException: The file format is not supported. No ImagePreloader found for file:///Users/mac/Desktop/image.png

But it is only inside of Jar! How it can be?

Upvotes: 2

Views: 1117

Answers (1)

Ivan
Ivan

Reputation: 73

At last I found out that the problem was in dependencies.
For some reason xmlgraphics-commons cannot resolve ImagePreloader if it is downloaded as transitive dependency of fop.
The solution was just change Gradle script from :

compile('org.apache.xmlgraphics:fop:2.1')

to:

compile('org.apache.xmlgraphics:xmlgraphics-commons:2.1')
    compile('org.apache.xmlgraphics:fop:2.1') {
        exclude group:'org.apache.xmlgraphics', module: 'xmlgraphics-commons'
    }

Upvotes: 2

Related Questions