Thomazzz
Thomazzz

Reputation: 203

fop 2 + xsl = Image not found

When moving from one server to another, pictures no longer appear when generating pdf using fop 2 + xsl

Trying to understand someone else's code when generating pdf using fop 2 + xsl I get in the logs

[FOUserAgent] Image not found. URI: test/myimg2/logo.png (No context info available)

relative paths are specified in xsl, the full path to the image will be /myserv/mydir/test/myimg2/logo.png

I read that in version fop 2 you can set the base url using FopFactoryBuilder

FopFactoryBuilder builder = new FopFactoryBuilder(new File("/myserv/mydir/").toURI(), resolver);

However, in my code the following construct

FopConfParser parser = new FopConfParser(new File(fopBaseDir, "userconfig.xml"));
FopFactoryBuilder builder = parser.getFopFactoryBuilder();

fopBaseDir - fop library directory
fonts are specified in userconfig.xml

builder is already created using userconfig.xml

Can you please tell me how can I specify the base URL for images? Can this be done in userconfig.xml?

i don't use servlets, JSF

I debugged the code and saw that after creating the fop object, BaseUri leads to the userconfig.xml file

fop -> foUserAgent -> ResourceResolver -> BaseUri = file:/myserv/etc/tomcat/fop/userconfig.xml

Is this normal? Or is the BaseUri not the base url that is used to search for images?

Upvotes: 0

Views: 410

Answers (2)

Thomazzz
Thomazzz

Reputation: 203

One of the working options when using the configuration file is to specify the base url settings there

<base>...</base>
<font-base>...</font-base>

Upvotes: 1

Conal Tuohy
Conal Tuohy

Reputation: 3215

I notice you've tagged the question with xslt; that suggests to me you might be willing to use an XSLT to pre-process your XSL-FO document and fix the image resource URLs, perhaps by using a template that matches fo:external-graphic/@src and prepends your image base URI, or just adds an xml:base attribute, e.g.

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <!-- supplement external-graphic's @src with a base URI -->
    <xsl:template match="fo:external-graphic/@src">
      <xsl:copy/>
      <xsl:attribute name="xml:base">file:/myserv/mydir/</xsl:attribute>
    </xsl:template>
    
   <!-- copy everything else unchanged -->
   <xsl:template match="node()|@*">
       <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
       </xsl:copy>
   </xsl:template>
   
</xsl:stylesheet>

You could even just set an xml:base attribute of /myserv/mydir/ on the XSL-FO document's root element, but beware that would also affect other relative hyperlinks in the document.

Upvotes: 1

Related Questions