Tutorials : Using FOP With Java - Graphics with FOP :

Using FOP With Java - Part 3
Graphics with FOP

Introduction

Last month, we saw how to implement a Print solution to print the FOP generated documents. We have also seen how a windows default printer can be used and how the output can directly be piped into a UNIX print queue. So far, we have discussed how to produce text based documents. However, in real life documents often need to embed images and graphics. In this article, we will examine the graphics capability of the FOP solution.

FOP has a wide range of graphics support. In short, it supports the following image formats:

  • BMP (Windows Bitmap).
  • EPS (Encapsulated Postscript).
  • GIF (Graphics Interchange Format).
  • JPEG (Joint Photographic Expert Group).
  • PNG (Portable Network Graphics).
  • SVG (Scalar Vector Graphics).
  • TIFF (Tag Image File Format).

All the above mentioned image formats are reasonably well- supported within FOP. However, different image formats suffer from limitations depending upon the generated output document formats. These limitations are in the areas of resolution of images, the scaling of images, color space and color profiles. These limitations are the only demerit, if you're really dealing with advanced graphics. For day to day use, the most commonly used formats are color spaces and color profiles. These are very well supported by FOP. In this article, I will not try to focus on the limitations but focus on how we can use the most commonly used formats of graphics with FOP Generated documents.

Using Graphics from External Resource

XSL-FO provides a mechanism to load graphics from external resources. These external resources are typically an image file residing in the local or remote address. The source image is specified by the src attribute whose value is a URI. It handles HTTP, FYP data and file system resource locators in URI format. 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. For example, you can refer to an image resource by c:\images\test.gif (absolute path) or you can access the same resource by specifying "test.gif" so long as the \images directory is present in the source XSL-FO document path. You can also access the same image over HTTP as http://server/images/test.gif . The important thing is that the resource should be available at the time of FOP transformation. This is crucial as after generating the document, you can decide to delete or move the image resource.

In general, we will load an external graphics in XSL-FO by using the <fo:external-graphic> element. The <fo:external-graphic> flow object is used for a graphic where the graphics data resides outside the fo:element tree. This element supports the following most commonly used properties:

  • Height: the height of the displayed image.
  • Width: the width of the displayed image.
  • Src: the source (url) of the image.
  • Alignment-baseline: the alignment of the image.
  • Scaling: (Uniform) means the original height width ration ispresderved.

Here is an example of how to load an external graphic using XSL-FO:

 
<fo:block>
           <fo:external-graphic src=”testImage.gif”/>
</fo:block>

This will load and display the testImage.gif in the defined block region provided the image is available in the classpath for the FOP transformation.

Another way of embedding images with XSL-FO is to used <fo:instream-foreign-object> element. The <fo:instream-foreign-object> element is used for inline graphics or other “generic” object where the object data resides as descendent of the <fo:instream-foreign-object>, typically as an XML element sub tree in a non-XSL namespace. One popular use of <fo:instream-foreign-object> is to embed Scalar Vector Graphics (SVG) files. The SVG in lay man's terms is a XML representation of an image. The <fo:instream-foreign-object> also supports all the common properties supported by the <fo:external-graphic> element. For a detailed description of all the attributes supported by these elements please refer to the W3C XSL-FO website.

Here is an example of loading a SVG file in XSL-FO:

<fo:instream-foreign-object>
     <svg:svg xmlns:svg=http://www.w3.org/svg@ width=500px” height=”500px”>
     <xvg:title> An example…</svg:title>
        …………….
</svg:svg>
</fo:instream-foreign-object>

Thus, there are in total, two ways to display and load images from external file using XSL-FO and FOP. One as an external graphic resource and second as an instream foreign object for images such as SVG. Both these approaches require that the image resource is a file local or remote to the processor. This begs the question, how to load images from a non-file resource. In the next section we will examine how to load images from a non- file resource using XSL-FO.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.