1
votes

I have a template (.jrxml) file. In that I have below code snippet which I used to include an image,

<image>
    <reportElement mode="Transparent" x="0" y="0" width="280" height="60" backcolor="#FFFFFF" uuid="d312321a854-323232332-223121d12c2d7f"/>
    <imageExpression><![CDATA["/home/name/app/test/reportTemplates/images/company_logo.JPG"]]></imageExpression>
</image>

So each time I change the deploying environment I need to change this /home/name/app/test/reportTemplates/images/company_logo.JPG path as well.

Any clear way to avoid that design ?

1

1 Answers

1
votes

The "normal" way to this is to use a parameter that contains the base path

<parameter name="IMAGE_DIR" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA["/home/name/app/test/reportTemplates/images/"]]></defaultValueExpression>
</parameter>

Set the defaultValueExpression so that you can use it in preview (iReport, JasperSoft studio) and isForPrompting="false" to avoid that it is asked everytime you preview.

Then in the imageExpression use the parameter as base path

<imageExpression><![CDATA[$P{IMAGE_DIR} + "company_logo.JPG"]]></imageExpression>

When is time to fill the report (application run-time) set the IMAGE_DIR parameter to the correct deployed path passing it in the parameter map.

es. if you are using java GUI you like a relative path to where you are installed

File imageDir = new File("images");
String imageAbsolutePath = imageDir.getAbsolutePath() + File.separator;

es. if you have web application (jsp/jsf) you like to find the a WEB-INF path of your deployed application

ServletContext context = request.getServletContext(); 
String imageAbsolutePath = context.getRealPath("/WEB-INF/images") + "/";

Now that we have the absolute path of our image directory we add it to the parameter map that we pass to the fillManager.

Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("IMAGE_DIR",imageAbsolutePath);
//..here goes all the other parameter you may set.

and then just pass the parameter map to the fillManager...

NOTE: this is an example I do not know how you fill

JasperPrint print = JasperFillManager.fillReport(report, paramMap, connection);