Wednesday, June 20, 2012

Jasper Reports Images in HTML PDF and XLS

One of the quickest options to get images rendered in Jasper Reports whether it is HTML, PDF or XLS is to use the "isLazy" image attribute combined with a absolute URL in the imageExpression node.

The report will render images as usual if run as XLS or PDF (embedded images) but when run as HTML the image will render from the absolute URL which is used of course in the "src" attribute.

So consider the below simple JRXML:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="" xmlns:xsi="" xsi:schemaLocation="" name="remoteImage" language="groovy" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
 <property name="ireport.zoom" value="1.0"/>
 <property name="ireport.x" value="0"/>
 <property name="ireport.y" value="0"/>
  <band splitType="Stretch"/>
  <band height="79" splitType="Stretch">
   <image isLazy="true">
    <reportElement x="314" y="27" width="62" height="50"/>
    <reportElement x="106" y="51" width="151" height="20"/>
    <text><![CDATA[Here is my Logo:]]></text>
  <band height="35" splitType="Stretch"/>
  <band height="61" splitType="Stretch"/>
        <band height="125" splitType="Stretch"/>
  <band height="45" splitType="Stretch"/>
  <band height="54" splitType="Stretch"/>
  <band height="42" splitType="Stretch"/>
It will correctly render the logo from the URL when used for any of the formats. For html in specific you will get something like:
<img src="" style="position:absolute;left:0px;top:0px;height:50px" alt=""/>
The advantage of this method besides simplicity is that static images can be kept in a central repository server which means an image change can be distributed to Jasper Reports or other pages in the web application. The typical example would be skinning a client website for which you need the logo in web pages but also in Jasper Reports.

The main disadvantage is conceptual: A report should be a snapshot and clearly a link is dynamic (the image can change any time in the future). In addition the images server has to be available for debugging purposes.

The alternative method is to not use lazy image loading in which case the image is pulled from a local path for XML and PDF but then for HTML a local servlet takes care of rendering the images which are stored previously in the user session. Here is what is needed in web.xml:
<!-- JasperReports Servlet to render local images for HTML EXporter -->
Here is what what is needed in your Controller when rendering XHTML/HTML Exporters:
request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "/jriservlet/image?image=");
Here is how the html source code will look like. The numbers refer to the index of images collection saved in user session:
<img src="/jriservlet/image?image=img_0_0_0" style="position:absolute;left:0px;top:0px;height:50px" alt=""/>

No comments: