Thursday 2 August 2012

Step by step guide for adding dynamic images from database to jasper report

Protected by Copyscape Originality Checker

Introduction

Reason we land-up here is to get answers to the question like:

  • 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:

  1. iReport 4.5.1
  2. Sql database with demo table
       Exa:
       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.
Enjoy....


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 

Protected by Copyscape Online Plagiarism Tool

30 comments:

  1. Dear sir
    when i compile this ireport then show error plz give me solution of this problem.
    I will be thankful to you always............
    Rohit Pachouli

    ReplyDelete
    Replies
    1. rohit can you pls provide me the table structure and error that you are getting on console ...

      Delete
  2. i am use Mysql database and use blob datatype but this show error...

    ReplyDelete
  3. thanks a lot! you are helping me a lot!

    ReplyDelete
  4. Hi,

    I 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...

    ReplyDelete
    Replies
    1. Mr. SK
      its 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]

      Delete
  5. 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)
    Thank you.

    ReplyDelete
    Replies
    1. 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

      for any further help contact me

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
  7. I use Ireport 5.5 but when I click preview, it's show me the blank page, please help me

    ReplyDelete
    Replies
    1. 1. any error log ?
      2. what database ?

      Delete
    2. 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"

      Delete
    3. Can you tell me the data type of the field u r using to store image data.

      Delete
    4. This comment has been removed by the author.

      Delete
    5. datatype in table is VARCHAR(45) but the image is JPEG.

      Delete
    6. Pls use Blob datatype to store image

      Delete
    7. I change datatype to Blob(100) but it's has the same output that I ask you in 1st question, please help me

      Delete
    8. pls answer following:

      1. 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)?

      Delete
    9. 1. I upload the image from PHP application.
      2. I can retrieve the image on my web app.

      Delete
    10. but my image is in folder and I can query it

      Delete
    11. here is the 1st issue,
      the tutorial above is given for the image that are stored in database as binary.

      Delete
    12. RDX i need ur help please,
      i 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)

      Delete
    13. 1. is image stored in database in binary format?
      2. any error logs

      Delete
  8. Thank you very much for your advise.

    ReplyDelete
  9. Hi RDX I am following all the above points i.e
    1. 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.

    ReplyDelete
  10. i use sqlite database and i insert image from java as byte[]
    the field image is blob type i follow all the steps but when i press preview show me error please some one help me !!

    ReplyDelete
  11. great tutorial you can also find some good resource related to ireport here:

    ReplyDelete
  12. Hi,

    I'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

    ReplyDelete