Image expression URL in Jasper Reports

2019-01-15 06:37发布

问题:

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.

回答1:

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



回答2:

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.



回答3:

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>


回答4:

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



回答5:

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()


回答6:

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>