Chapter 7 of the XSL-FO Tutorial
7. Graphics
There is a special inline element for including graphics into XSL FO — <fo:external-graphic>. The source image is specified by the src attribute whose value is a URI. IREn handles HTTP, FTP, data and filesystem resource locators in URIs. An unqualified URI is treated as a path to a file in the local file system; if the path is relative, it is calculated from the location of the source XSL FO document.
Here's an example:
<fo:block>
This text includes a picture:
<fo:external-graphic src="url('smile.gif')"❶
content-height="1em"❷ content-width="1em"❸/>
</fo:block>
| ❶ | Note the url('…') function-like wrapper around the file name: this is required by the XSL 1.0 Recommendation. (IREn recognizes unwrapped URLs, too). |
| ❷ ❸ | In this example, the height and the width of the image are expressed in units relative to the nominal font size. This is a convenient technique to scale small inlined images proportionally to the text height. |
If you need a block-level graphic, you should wrap the element in a <fo:block>, like in the example below:
<fo:block font-weight="bold"
keep-with-next.within-column="always">❶
Figure 1: A Smiling Face
</fo:block>
<fo:block>
<fo:external-graphic src="url('smile.gif')"
content-height="200%" content-width="200%"/>❷
</fo:block>
| ❶ | This property “glues” the figure caption to the figure itself. |
| ❷ | content-width and content-height expressed as percents denote scaling the image from its original (intrinsic) size. |
It is also possible to create an image directly in the XSL-FO, using the embedded SVG feature:
<fo:block>
Here is the image of a typical roadsign:
<fo:instream-foreign-object content-height="1em">❶
<svg:svg xmlns:svg="http://www.w3.org/2000/svg"❷
height="100" width="100" viewBox="-50 -50 100 100">
<svg:circle r="50" style="fill:red; stroke:none"/>
<svg:rect x="-40" y="-10" width="80" height="20"
style="fill:white; stroke:none"/>
</svg:svg>
</fo:instream-foreign-object>
</fo:block>