Image expression URL in Jasper Reports

Mark Estrada picture Mark Estrada · Sep 2, 2010 · Viewed 62.3k times · Source

I would like to embed an image into my JasperReports jrxml file. I have this directory structure.

Tomcat Root
 webapps
  reports
   -->images
    --> company_logo.jpg
   -->reports
    -->sample.jasper
   -->WEB-INF
    -->classes

And I tried doing this thinking that this is relative to my reports context root

<image>
 <reportElement x="0" y="0" width="104" height="40"/>
 <imageExpression class="java.lang.String">
  <![CDATA["images/company_logo.jpg"]]>
 </imageExpression>
</image>

..and this also

<image>
 <reportElement x="0" y="0" width="104" height="40"/>
 <imageExpression class="java.lang.String">
  <![CDATA["/images/company_logo.jpg"]]>
 </imageExpression>
</image>

but it always says there was an error loading bytes from location JRLoader.

I tried doing this and it works, but I am a bit confused why my first two attempt doesn't.

Is this really how you embed images in JasperReports? Do you need to supply the whole path? I am thinking that there should be a page relative something.

<image>
 <reportElement x="0" y="0" width="104" height="40"/>
 <imageExpression class="java.lang.String">
  <![CDATA["http://localhost:8080/reports/images/company_logo.jpg"]]>
 </imageExpression>
</image>

I am a bit puzzled about the correct way.

Answer

Giorgos Dimtsas picture Giorgos Dimtsas · Sep 10, 2010

<![CDATA["../images/company_logo.jpg"]]> should do the trick. The path is relative to your .jasper file.