Dynamic Image From Database in Jasper Report

   When dealing with internal documentation in a company, images plays an important role. For example, company logo is a must in most of the documentation in a company. Here I am going to explain how to create dynamic images in jasper report fetched from the database.

   I am using iReport 4.7.1 with OpenERP. In my report I am passing the image to the report as a parameter. I have created a parameter in iReport as “logo” which holds image data passed from the python to the report. In python side, i am fetching my logo as: 

and passed to report as a parameter “logo”. The variable class of logo should be  “java.lang.String”.

   In report add a Report Import directive “org.apache.commons.codec.binary.Base64“. To do that in iReport, right click on the report and go to “Properties” at the end you can see “Imports” as shown below:

Add the entry to it. Now create a variable say “photo”  with “Variable class” as “java.io.InputStream”, “Calculation” as “Nothing”, “Reset Type” as “Report” and “Variable Expression” as:

new ByteArrayInputStream(new Base64().decodeBase64($P{logo}.getBytes(“UTF-8”)))

where $P{logo} is the parameter created earlier which contain image data.

   Now drag and drop the image component from the pellet window. When the windows prompt to select the image, press cancel. Now in “Image Expression” field, add the variable $V{photo} created before and “Expression Class” as “java.io.InputStream”. If you are intending to put the logo in title band, set evaluation time to “Report”. Now the report should be printed with logo from the database.

Comments

Raza: Can you please point to a document or blog that would help setup jasper reports in OpenERP? I created the report in iReports and it runs fine on jasper server. In openerp, I created the "Jasper Document" and tried to launch the report but I get unexpected end of input. I took some hint from the NovaPoint document (as it is outdated), and saved the report as jrxml file under account_asset/report, and also created a account_asset_report.xml pointing to this report. Is there anything else that I am missing? Thanks".

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. paully says:

    thank you very much.

    1. Raza says:

      Can you please point to a document or blog that would help setup jasper reports in OpenERP?
      I created the report in iReports and it runs fine on jasper server. In openerp, I created the “Jasper Document” and tried to launch the report but I get unexpected end of input.
      I took some hint from the NovaPoint document (as it is outdated), and saved the report as jrxml file under account_asset/report, and also created a account_asset_report.xml pointing to this report.
      Is there anything else that I am missing?
      Thanks

  2. MuraliKrishnaReddy says:

    I am using the iReport4.0.2 and when i did the above things and try to create a variable, i got an Error as :
    Error filling print… Error evaluating expression :      Source text : new ByteArrayInputStream(new Base64().decodeBase64($F{logo_web}.getBytes(“UTF-8”)))
    net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression :      Source text : new ByteArrayInputStream(new Base64().decodeBase64($F{logo_web}.getBytes(“UTF-8”)))
    .
    .
    .
    Caused by: groovy.lang.MissingMethodException: No signature of method: [B.getBytes() is applicable for argument types: (java.lang.String) values: [UTF-8] Possible solutions: getAt(java.lang.String), getClass(), getAt(groovy.lang.IntRange), getAt(java.lang.Integer), getAt(groovy.lang.ObjectRange), getAt(java.util.Collection)

    1. Excellent post but I was wondering if you could write a litte more on this subject?
      I’d be very grateful if you could elaborate a little bit more.
      Appreciate it!

  3. Kyaw Sint says:

    I’m using iReport 5.0.1. I can’t show dynamic image with pdf format using ireport and jasper report in openerp. I have been tested above you say but doesn’t work. And then my iReport doesn’t work preview. I click preview button ‘null message box’ show. So how can i show dynamic image and work ireport preview button. Thaks you! Waiting for your reply . . .

  4. shrikanth says:

    Hi,
    I have followed above mentioned steps.Unfortunately I couldnot see my images in ireport. However no errors encountered. It will be great if you provide me your email id so that I can send you screenshots . Appreciated your help

  5. shrikanth says:

    Kindly share your email id to me so that I can send my screenshots to you.
    Thanks
    Shrikanth

  6. shrikanth says:

    Hello,
    Followed the above mentioned steps for my openerp/ireport project.
    Unfortunately couldn’t get image on to report. However no errors are been reported

      1. shrikanth says:

        Thank you. I have sent you the mail with screenshots.

        1. Prajul P T says:

          Hi,

             I found the issue. The problem is if the data is not present ie; if the query in the report returns no data in ireport, the variables in the ireport will not be executed. So what i did is:
          1. Moved the Image pallet to Title band
          2. Changed "When No Data" property of the report to "All Sections, No Detail"
          3. In Image pellet property, changed "Image Expression" to "new ByteArrayInputStream(new Base64().decodeBase64($P{logo}.getBytes("UTF-8")))"
          1. shrikanth says:

            Hi,
            I followed above mentioned steps but no luck .
            However no errors reported .
            thanks
            Shrikanth

  7. Anonymous says:

    Thanks for your post!
    I wannted this dynamic image in my report!
    I didn’t know how to do that. Now I got it.
    I appreciate you 🙂

© 2020 Zesty Beanz Pvt Ltd All Rights Reserved.