22

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mark Estrada
  • 9,013
  • 37
  • 119
  • 186

7 Answers7

34

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

Giorgos Dimtsas
  • 12,019
  • 3
  • 29
  • 40
  • A point to be noted is that the file-names and paths are case-sensitive! – jagbandhuster Jan 09 '12 at 04:11
  • 1
    This did not work for me when Spring was compiling the Jasper report files on the fly. I had to resort to the technique described [here](http://stackoverflow.com/questions/5376725/using-ireport-with-a-relative-path) – Stefan Haberl Aug 14 '12 at 09:25
14

You need to create a PARAMETER of type String called for example CONTEXT, and from your JSP send the servlet context:

parameters.put("CONTEXT",this.getServletContext().getRealPath("/"));

Now, in your report you use the parameter:

$P{CONTEXT}.toString()+"reports/images/logo.png"

The same apply for sub reports or other web resources, example:

$P{CONTEXT}.toString()+"reports/OrdenCompraAlmacen_Items.jasper"

I hope it is useful.

Manuel Gaytan
  • 141
  • 1
  • 3
  • Works like a charm! – Nik Mar 02 '18 at 19:21
  • it worked to me!!! thanks!! even if i load the path from database: hmParams.put("CONTEXT",pathReceta.getValor()); then I need to add a extra "/" like this: $P{CONTEXT}.toString()+"/reports/images/logo.png" – Mintakastar Jul 26 '19 at 02:51
9

With .jrxml when use an absolute path with image, when the packaged jar filr will be deployed you will get:

java.lang.IllegalArgumentException: name
at sun.misc.URLClassPath$Loader.findResource(Unknown Source) ~[na:1.8.0_121]
at sun.misc.URLClassPath.findResource(Unknown Source) ~[na:1.8.0_121]
at java.net.URLClassLoader$2.run(Unknown Source) ~[na:1.8.0_121]
at java.net.URLClassLoader$2.run(Unknown Source) ~[na:1.8.0_121]

Try to load resource as:

<imageExpression><![CDATA[this.getClass().getResourceAsStream("/img/mdg_logo.jpg")]]></imageExpression>
pdorgambide
  • 1,787
  • 19
  • 33
3

sometimes is better use File.separtor: "ima" + File.separator + "logo.jpg"

0

Use the below expression in the image Expression of jasper report IDE on image properties of the jrxml and the relative image path

Accordingly change w.r.t path

getClass().getResource("META-INF/resources/webjars/Bank/themes/default/images/Logo.png").openStream()
GhostCat
  • 137,827
  • 25
  • 176
  • 248
satish hiremath
  • 127
  • 1
  • 4
0

This way the path is relative to your current working directory of your project. "." means current working directory.

-1

Put your picture in the following structure:

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

<imageExpression class="java.lang.String">
    <![CDATA["company_logo.jpg"]]>
</imageExpression>
David Gorsline
  • 4,933
  • 12
  • 31
  • 36
  • English please. [so] is an English-only website. You can go to [a51] to see if there's a site or proposal for one in your language. – Bernhard Barker May 08 '14 at 18:33