Introduction
Reason we land-up here is to get answers to the question like:
Most of the time images are stored in database with predefined data type [binary, blob, Image]. Retrieving such images to the jasper report is one of the most common and confusing task encountered in dynamic reporting implementation process. Following article will provide you a guide to resolve this need with simple steps.
- How to add dynamic image from database in jasper report?
- How to get image from database on to jasper report?
- How to get image stored in bites to jasper report?
- etc ...
Most of the time images are stored in database with predefined data type [binary, blob, Image]. Retrieving such images to the jasper report is one of the most common and confusing task encountered in dynamic reporting implementation process. Following article will provide you a guide to resolve this need with simple steps.
Note: It is considered that user referring this document have basic knowledge of creating simple jasper reports with iReport tool.
So here we go:
Prerequisite:
- iReport 4.5.1
- Sql database with demo table
User_Name -> [nvarchar]
USer_Photo -> [Image]
Step 1: [ Create Blank Report ]
- Open iReport tool [ i am using iReport version 4.5.1]
- Go to File -> New
- [ 1. Choose Template ]: In New File window select “Blank A4”(A) and click on “Lunch report wizard”(B) button
- [ 2. Name and Location ]: Specify report name, Location to save on hard disk, Then click next.
- [ 3. Query ]: At this step you need to provide Connection/data source(you an select from drop down list (A) OR can create new from "New" Button (B)). Then you need to specify the query which will be retrieving data from required table (Query can be written manually (C) OR can be crated with "Design Query" (D) wizard) and click next.
- [ 4. Fields ] Select fields to be added in report (click ">>" (A)).Click next
- [ 5. Group By...] you can use this section to group clause for your data,(it is optional for this demo) click next and then on new window click Finish.
Step 2: [ Field Configuration ]
- After step 1 you will land-up in IDE with your report open in designer view. To the left you can see fields that you fetched from data base (A) and page design components(B).
- As you know the User_Photo field is have data type Image in data base, Here while mapping iReport assign java.lang.Object class to it by default. As we want it to show image with image with image element present in iReport we have to change its field class to java.io.InputStream. [Now select User_Photo field go to window menu -> select Properties OR press ctrl+shift+7.]
- Go to properties and change Field class to java.io.InputStream
Step 3: [ Design Report ]
- Now drag and drop User_Name field from report inspector to Report designer view.
- Similarly drag and drop Image and Static Text element from palette.[in case palette window is not there press ctrl+shift+8. While dropping image element it will show new window to select file, just click cancel ]
- You can edit content in static text element and format it as per your need
- Now select image element (A), go to property window to select Image expression (B).
- Once you click button next to Image Expression, it will open new window. In new window bauble-click element which will provide data to display image [ here it will be "User_Photo"]. click OK
- Select Image element go to properties and change expression class from java.lang.String to java.io.InputStream
Step 4: [ Test Report ]
- Now click on preview tab to get report out-put
- And here you have it. Your 1st report with dynamic image data from database.
Tag : jasper, ireport, jasper report, dynamic image from database, image from database, image from database in jasper report, image in report, .jasper, dynamic jasper report with image
Dear sir
ReplyDeletewhen i compile this ireport then show error plz give me solution of this problem.
I will be thankful to you always............
Rohit Pachouli
rohit can you pls provide me the table structure and error that you are getting on console ...
Deletei am use Mysql database and use blob datatype but this show error...
ReplyDeletethanks a lot! you are helping me a lot!
ReplyDeleteHi,
ReplyDeleteI have followed above mentioned steps and come across following error
Exporting PDF (using iText) to file... C:\Documents and Settings\user\Desktop\report13.pdf!
Error exporting print... Image read failed.
net.sf.jasperreports.engine.JRException: Image read failed. at net.sf.jasperreports.engine.util.JRJdk14ImageReader.readImage(JRJdk14ImageReader.java:73) at net.sf.jasperreports.engine.util.JRImageLoader.loadAwtImageFromBytes(JRImageLoader.java:167) at net.sf.jasperreports.engine.JRImageRenderer.getImage(JRImageRenderer.java:407) at net.sf.jasperreports.engine.JRImageRenderer.getOnErrorRendererForImage(JRImageRenderer.java:310) at net.sf.jasperreports.engine.export.JRPdfExporter.exportImage(JRPdfExporter.java:1384) at net.sf.jasperreports.engine.export.JRPdfExporter.exportElements(JRPdfExporter.java:857) at net.sf.jasperreports.engine.export.JRPdfExporter.exportPage(JRPdfExporter.java:821) at net.sf.jasperreports.engine.export.JRPdfExporter.exportReportToStream(JRPdfExporter.java:698) at net.sf.jasperreports.engine.export.JRPdfExporter.exportReport(JRPdfExporter.java:420) at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:1174) at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572) at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)
Can you help on this plz...
Mr. SK
Deleteits seems there some issue in image read ....
please let me know 1st.
1. what is a data type you are using to store in data base.
2. what type of image you are storing in database.
3. are you able to display image on web page [just for a try]
Please how can you do the same this title (Step by step guide for adding dynamic images from database to jasper report) in dynamic reporting (hard coding in java)
ReplyDeleteThank you.
the above steps will help you create jrxml file for report. The integration with java is not a big tas. follow tutorial from http://community.jaspersoft.com/wiki/jasperreports-library-tutorial
Deletefor any further help contact me
This comment has been removed by the author.
ReplyDeleteI use Ireport 5.5 but when I click preview, it's show me the blank page, please help me
ReplyDelete1. any error log ?
Delete2. what database ?
It's appear Message Dialog Box when I click preview "Error displaying report page see the console for detail". But when I click "OK" and see in Report problem window it's blank, and when I see in report output it's appear "Finished"
Delete2. I use MySql
DeleteCan you tell me the data type of the field u r using to store image data.
DeleteThis comment has been removed by the author.
Deletedatatype in table is VARCHAR(45) but the image is JPEG.
DeletePls use Blob datatype to store image
DeleteI change datatype to Blob(100) but it's has the same output that I ask you in 1st question, please help me
Deletepls answer following:
Delete1. how u stored image in database(BLOB)? by uploading from client side OR static data by converting image in to binary?
2. Are you able to retrieve image on web page(just to test whether image is saved properly)?
1. I upload the image from PHP application.
Delete2. I can retrieve the image on my web app.
but my image is in folder and I can query it
Deletehere is the 1st issue,
Deletethe tutorial above is given for the image that are stored in database as binary.
RDX i need ur help please,
Deletei did what you did but after i upload .jrxml file it's not load the PDF (i'm working database for my website and i need to display image inside the report when generated)
1. is image stored in database in binary format?
Delete2. any error logs
Thank you very much for your advise.
ReplyDeleteHi RDX I am following all the above points i.e
ReplyDelete1. Image is saved as Blob in Db (Sqlite)
2. Expression for image field is set $F{Image1}
3. Imagedata field also changed from java.lang.Object to java.io.InputStream
4. On clicking Preview, report loads, aborts without any error message. Please help.
i use sqlite database and i insert image from java as byte[]
ReplyDeletethe field image is blob type i follow all the steps but when i press preview show me error please some one help me !!
thanks for sharing info. keep it up
ReplyDeletegreat tutorial you can also find some good resource related to ireport here:
ReplyDeleteHi,
ReplyDeleteI'm using oracle DB. I've followed the above steps you mentioned. While preview, logo is not displaying in the report.
Can you please help me sort out this issue.
Thanks