16

I designed a jasper report using ireport designer in which I added logo image in the title of the report. This image is added from the hard coded path on the local machine. I need to add the logo image from my projects classpath. To do that I created a parameter for the image in the report which is supplied from the program.

InputStream imgInputStream = this.getClass().getResourceAsStream("header.png");

HashMap<String, Object> parameters = new HashMap<String, Object>();
parameters.put("dateFrom", datum1);
parameters.put("dateTo", datum2);
parameters.put("logo", imgInputStream);


strQuery = "Select calldate,src,dst,duration,disposition,cdrcost from cdrcost where date(calldate) between '" + datum1 + "' and '" + datum2 + "'";

rs = conexiondb.Consulta(strQuery);
JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs);
//JasperPrint jasperPrint = JasperFillManager.fillReport(reportStream, parameters);

JasperRunManager.runReportToPdfStream(reportStream, fos, parameters, resultSetDataSource);

And below is the image snippet from the report:

<image>
  <reportElement x="0" y="1" width="555" height="61"/>
  <imageExpression><![CDATA[$P{logo}]]>
  </imageExpression>
</image>
Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102
Amit
  • 233
  • 1
  • 5
  • 9

3 Answers3

45

We always pass in the image instead of the InputStream. First load up the image and set it in the parameter map:

BufferedImage image = ImageIO.read(getClass().getResource("/images/IMAGE.png"));
parameters.put("logo", image );

Then the parameter is just defined like:

<parameter name="logo" class="Object" isForPrompting="false">
  <parameterDescription><![CDATA[The letterhead image]]></parameterDescription>
  <defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>

And when placed in the report it looks like:

<image>
  <reportElement x="324" y="16" width="154" height="38"/>
  <imageExpression><![CDATA[$P{logo}]]></imageExpression>
</image>
Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102
  • 1
    Sir i did the same but it did not happen image is not loading from the classpath and pdf report is not generating – Amit Nov 30 '12 at 06:52
  • 1
    Sir image is not loading with this code and getting error "java.lang.IllegalArgumentException: input == null!" – Amit Nov 30 '12 at 08:50
  • I assume you replaced `"/images/IMAGE.png"` with `"header.png"`? Try `"/header.png"` if it is in the default package of the jar. – Jacob Schoen Nov 30 '12 at 13:51
  • Hi jschoen I am new ireport. I just followed your instruction. Created a parameter field in ireport and change to Object and when i drag and drop in the title, it looks as follows <![CDATA[""+$P{logo}]]> But in your example, it as shown as image. how ? – Senthil Muthiah Jul 17 '13 at 04:18
  • 1
    You need to swap out the `textFieldExpression` tag with the `imageExpression`. – Jacob Schoen Jul 17 '13 at 13:21
  • Thank you so much, that worked for an image embedded inside the jar. – membersound Nov 26 '15 at 14:42
  • Did the same but getting : et.sf.jasperreports.engine.JRException: Byte data not found at: ((Object)parameter_background.getValue()) – bashizip Mar 07 '22 at 12:14
5

You can easily get the URL form the classpath/classloader. This is a valid input for <imageExpression> and therefore you can use it to embed an image in your pdf. The following worked for me:

Setting the parameter:

URL url = this.getClass().getClassLoader().getResource("pdf/my_image.tif");
parameters.put("logo", url);

Declaration in the report:

<parameter name="logo" class="java.net.URL">
    <defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>

Usage in the report.

<image>
   <reportElement x="100" y="30" width="135" height="30"/>
   <imageExpression><![CDATA[$P{logo}]]></imageExpression>
</image>

Some additional observations

  • Before I was using InputStream and it worked fine when displaying the image only once. When I needed to repeat the image, InputStream did not work because the stream is consumed on the first display so it can not be used after that. I did not find an easy way to reset it.
  • I found out that URLs could be used from here: http://jasperreports.sourceforge.net/sample.reference/images/index.html
gus3001
  • 851
  • 9
  • 19
2

I did not manage to get it working with any of those methods, I was having the error :

 Error evaluating expression for source text

at compiling the report in java.

In java you have to get your image to an inputstream so either

byte[] image = imageRepository.getLogo();
InputStream logo= new ByteArrayInputStream(image);
parameters.put("logo",logo);

because I am getting the image as a byteArray from a database, but if you have it somewhere in your JAR:

ResourceLoader resourceLoader;
InputStream logo= resourceLoader.getResource("classpath:/image/logo.jpg").getInputStream();
parameters.put("logo",logo);

Then in the in jrxml it simply gives :

    <parameter name="logo" class="java.io.InputStream"/>

    <image scaleImage="RealSize">
        <imageExpression><![CDATA[$P{logo}]]></imageExpression>
    </image>
Vinz
  • 329
  • 5
  • 12