Sunday, 16 August 2015

Base64 Image in a Jrxml file

Sometimes we are dealing with Base 64 encoded images and let's say we need to include it in our jrxml file for exporting to pdf. I encountered a similar problem today, and while it seems easy at first, it can be very intruding and frustrating if you are unable to find the right moves. Here are the steps I used to solve the problem.


  1. Create an image tag <image>.
  2. Inside the <image> tag, create a <reportElement> tag. This tag can be used to add attributes to the the image like its position and size.
    Example :
    <reportElement x="437" y="85" width="92" height="108" uuid="7330c3ba-a8ed-4bfa-bbe6-a008c9e6558c"/>
  3. Finally, add an <imageExpression> tag as a sibling to the <reportElement> tag. The data inside the <imageExpression> tag is where you put your base64 image, and here is where you need to be careful. Otherwise you will keep beating the bush without finding the image on your pdf.
    • Put the base 64 string inside the CDATA block.
    • Remember to enclose the string withing quotes.
    • Do not put the html image renderer data:image/jpeg;base64,as a prefix to your base64 string.
    • Enclose the data within new java.io.ByteArrayInputStream(new sun.misc.BASE64Decoder().decodeBuffer())for decoding the base64 string.
An example: 


<image onErrorType="Icon">
  <reportElement x="437" y="85" width="92" height="108" uuid="7330c3.."/>
  <imageExpression>
<![CDATA[new java.io.ByteArrayInputStream(new sun.misc.BASE64Decoder().decodeBuffer(
"/9j/4AAQSkZJRgAB ......"))]]></imageExpression>
</image> 

No comments:

Post a Comment