4

JasperReport requires (by default) that images be in "WEB-INF/classes/". I'd like to share images between PDFs and normal JSP pages. I'd rather not clutter up classpath with garbage image files. How do I force JR to use a different location for images?

Justin Wrobel
  • 1,981
  • 2
  • 23
  • 36

3 Answers3

5

I recommend creating the following report parameters:

  • ROOT_DIR -- "/reports/"
  • IMAGE_DIR -- $P{ROOT_DIR} + "images/"
  • STYLE_DIR -- $P{ROOT_DIR} + "styles/"
  • SUBREPORT_DIR -- $P{ROOT_DIR} + "subreports/"
  • COMMON_DIR -- $P{SUBREPORT_DIR} + "common/"

This allows the images directory to be relative to the ROOT_DIR path. It also allows you to change ROOT_DIR dynamically. The parameters must be declared in their relative order.

In your case, using an absolute path:

  • ROOT_DIR -- "/home/user/"
  • IMAGE_DIR -- $P{ROOT_DIR} + "Pictures/"

Note that changing between operating systems, directory structures, environments (e.g., migrating to JasperReports Server from JasperReports & JSF) and so forth, can be accomplished without having to modify the report. (Well, some modifications are required for JasperReports Server.)

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
2

Following @Dave's advice I split out the base directory into a Parameter and gave it a default. That way I can specify either an absolute path, classpath or url. Here are the relevant pieces of code:

Controller:

modelMap.put("ROOT_DIR", "/srv/images/");

-Or-

modelMap.put("ROOT_DIR", "http://site.com/images/");

jrxml File:

<parameter name="ROOT_DIR" class="java.lang.String">
    <defaultValueExpression><![CDATA["/home/user/Pictures/"]]></defaultValueExpression>
</parameter>
...
<imageExpression class="java.lang.String">
   <![CDATA[$P{ROOT_DIR}+"leaf_banner_red.png"]]>
</imageExpression>
Justin Wrobel
  • 1,981
  • 2
  • 23
  • 36
1

Upon further inspection, it doesn't seem like I can set a 'location' for images anywhere but I did find out that you can specify images 1 of 3 ways:

  • Classpath: "image.png"
  • Absolute: "/home/user/Pictures/image.png"
  • Url: "https://encrypted.google.com/images/logos/ssl_ps_logo.png"
Justin Wrobel
  • 1,981
  • 2
  • 23
  • 36