In this post, I will explain how we can add images using dynamic path in Jrxml. First of all, we need to add the image component to JRXML file. Click on Window Menu> Palette and then drag-drop the image component on the designer window.

jasper1

 

Once we add image component we can add a parameter which will receive the path value while creating jrxml report object from grails.

jasper-add-param

 

After adding parameter we select image > select image expression property:

 

jasper-image-exp1

Click Edit button on the right and add the expression value:

 

jasper-image-exp2

 

In grails we can put the image file in /web-app/images folder  and all we have to do is to pass the path of the image as parameters. We can obtain the full path of the application’s image folder using servletContext(Documentation) like this:

 


def realPath = servletContext.getRealPath("/images/")
params.dynamicPath=realPath

Now we can pass the params to jrxml as parameters like this:


 def reportDef = new JasperReportDef(name: 'fileName.jrxml', reportData: objectsList,
 fileFormat: JasperExportFormat.PDF_FORMAT,parameters:params)

 

After creating the JasperReportDef object we can generate the report using jasperService.generateReport(reportDef).

 

That was all! Hope it helps 🙂