If your creating your XML dynamically using Java you can do:
then write the Base64 encoded data in your XML as:
As the post says, in your JasperReport (we currently use iReport as a report editor) define a String field, let’s call it
Image, for the image:
then define a variable, let’s call it
ImageBytes, which uses that field:
with the Variable Expression:
(don’t forget to add
org.apache.commons.codec.binary.Base64 to the imports of the report properties)
and in the image element on your layout use the ImageBytes variable:
and you should see your image properly rendered in the final report.
However, what I found at this point is that while the images displayed fine in iReport in PDF preview most PDF readers were unable to display the images, resulting in just a black rectangle.
The solution was to change the
ImageBytes variable class to
java.awt.Image with a constructor of:
then change the expression class of the image element on the layout to
java.awt.Image as well. You’ll also have to add
javax.imageio.ImageIO to the imports of the report.
This technique can be useful when you need to grab the image data dynamically (not from actual files on the filesystem), you can’t get each image from a URL (perhaps due to security constraints), and don’t want to use a custom Java datasource.