csspdfxsl-fo

Images not displaying in PDF when using xsl-fo


I am new to the whole Java programming having inherited code from someone. I have set up my pc with Tomcat, eclipse, maven, etc. and everything works fine.

The problem is I am using a stylesheet to create a PDF report, this works great on my PC but when I deploy the war file to my server, I don’t get the logo on my PDF. The PNG is there and if I type the URL manually it appears in the browser.

This is my code, which runs fine on my PC using Tomcat, fop-2.1, and Batik 1.8.

<fo:static-content flow-name="xsl-region-before">
      <fo:block width="100%" text-align="center">
        <fo:external-graphic content-width="scale-to-fit" width="50%" scaling="uniform">
          <xsl:attribute name="src">
            url('http://localhost:<xsl:value-of select="/endOfProjectReport/contextPort" /><xsl:value-of select="/endOfProjectReport/contextPath" />/resources/images/Logo.png')
          </xsl:attribute>
        </fo:external-graphic>
      </fo:block>
    </fo:static-content>

I have tried replacing the URL with url('http://localhost:8080<xsl:value-of select="/endOfProjectReport/contextPath" />/resources/images/Logo.png')

I have even tried using the full external URL.

If I put a hyperlink to the logo in the pdf that displays the logo.


Solution

  • I have fixed this issue. There is nothing wrong with the code, it does not matter if you use a relative path or not, both will work. I setup a new server and it worked first time no issues. I then installed the SSL cert, along with the required changes to redirect the port in the server.xml file and got exactly the same problem. The issues is with the redirect and the setup of the server.xml file but the solution is in the web.xml file. For some reason it prevents the localhost from resolving the url to where the images are stored. There are two solutions. You can either move the image file to the root of C: and change the code below to use the root path.

    <fo:static-content flow-name="xsl-region-before">
        <fo:block width="100%" text-align="center" font-size="28pt" font-weight="bold">
           <fo:external-graphic content-width="scale-to-fit" width="50%" scaling="uniform" src="/Logo.png"/>
        </fo:block>
    </fo:static-content> 
    

    Or you can change the web.xml file to allow prevent the SSL cert from being applied to the images folder. Which is my preferred option.

    Add this to the end of the web.xml

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Allow Application</web-resource-name>
                <url-pattern>/resources/images/*</url-pattern>
            </web-resource-collection>
        <user-data-constraint>
            <transport-guarantee>NONE</transport-guarantee>
        </user-data-constraint>
    </security-constraint>
    

    This will allow the images to be accessed by the localhost, without redirecting the port as per the server.xml file. This was the problem, the code was using http and the server.xml redirected the port which meant the images could not be resolved. Doing it manually on a browser worked for some reason but in the stylesheet it does not, and no error where given in the log files