Showing database images in Jasper Report from Servoy

We can show image in Jasper Report in different ways.

1. Showing static image placed in hard drive :

– Create a jasper report file in iReport. – Place an image field in this report.

– Set the “Image Expression” property of this field to the url of the image.

For example : “C:\\report\\logo.jpg”

2. Passing database image through parameter to Jasper Report from Servoy :

– In the report place an image field. – Set the “Image Expression” property with a custom parameter.

eg : $P{source_logo}

– In servoy create a temporary file of the database stored image. – Pass the absolute path through parameter to the jasper report. – Call the runReport method to show the report.

– Delete the temporary file.

Syantax :
//Create a temporary file
var tempFile = plugins.file.createTempFile('temp_file_name', '.jpg');

//Put the database media field content in this temporary file
plugins.file.writeFile(tempFile, db_image_field)

//Run report
plugins.jasperPluginRMI.runReport(foundset, ‘report.jasper’ , null, ‘view’, {source_logo: tempFile.getAbsolutePath()});

– Here we are passing forms foundset to the Jasper Report. To learn more about passing foundset to jasper report, click here. – In the last parameter we are sending absolute path of the temporaryly created file to the jasper report with the name “source_logo”. – Since this parameter is used as Image Expression in the report, it will display the image at run time.

– Now you can delete the temporary file by-

plugins.file.deleteFile(tempFile.getAbsolutePath());
3. Directly placing the image field in the report :

– This is the best way to show image in jasper report. – Go to the properties of the database image field. Change its “Field class” Property to “java.io.InputStream”. – Place a image field in the report. – Change the “Image Expression” of this image field to the database field name. eg : $F{db_image_field}

– Change it’s “Expression Class” to “java.io.InputStream”.

Now you can run the report and see the image in the report.
Which of the above approaches you use to show images in Jasper Report in specific instances depends on the requirements.

150 150 Burnignorance | Where Minds Meet And Sparks Fly!