Static Images in PDFs - quality-manager/onboarding GitHub Wiki
Background
Just like with any UI, sometimes you'll want to insert icons or other images into a PDF. These are not user added images, but assets that we have in our own repository.
In XSL-FO we use the external-graphic
element to insert an image into the PDF. This element works much like the img
element in HTML. Just like img
we'll use the src
attribute to describe the location of the desired image. The problem is that Apache FOP (our XSL-FO processor) doesn't know how to find files in an OSGi environment. As such, simply using a traditional file path will result in FOP failing to find the image.
The Right Way
The Export Framework contains a set of ResourceResolver
s (formally UriResolver
s) that FOP can fall back on when it doesn't know how to find an image. In particular, FileResourceResolver
is a QM class that knows how to find files via OSGi APIs.
To leverage this capability you'll need to use a special syntax for your file path.
<fo:external-graphic src="{'basecache://com.ibm.asq.common.web/resources/ui/internal/images/icons/elcl16/calculatedrisk_obj.gif'}"/>
There are a couple things to note in the above example:
- The URI begins with the oddly named "basecache" protocol (I have no idea why this name was used)
- The second part of the URI is the name of the OSGi bundle that the file is contained by
- The last part of the URI is the path to the file relative to its containing bundle