Reputation: 2019
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?
Upvotes: 4
Views: 5414
Reputation: 31171
I recommend creating the following report parameters:
"/reports/"
$P{ROOT_DIR} + "images/"
$P{ROOT_DIR} + "styles/"
$P{ROOT_DIR} + "subreports/"
$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:
"/home/user/"
$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.)
Upvotes: 5
Reputation: 2019
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>
Upvotes: 2
Reputation: 2019
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:
Upvotes: 1