3/22/2017 - 6:14 AM

Jasper Dynamic Images

Jasper Dynamic Images

// Snippet 1
Dynamic Image in Jasper Report
jrxml content

                <reportElement x="5" y="5" width="200" height="90"/>
                <imageExpression class="java.awt.Image"><![CDATA[$P{logo}]]></imageExpression>

Java Code to convert image from URL into BufferedImage

       URL url = new URL("http://localhost/image/image.jpg");

        // Read the image ...
        InputStream inputStream = url.openStream();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];

        int n = 0;
        while (-1 != (n = inputStream.read(buffer))) {
            baos.write(buffer, 0, n);
        byte[] imageInByte = baos.toByteArray();

        // convert byte array back to BufferedImage
        InputStream in = new ByteArrayInputStream(imageInByte);
        BufferedImage bImageFromConvert = ImageIO.read(in);

Add into Map to pass into jasper

Map inputParameters = new HashMap();
inputParameters.put("logo", bImageFromConvert);

// Snippet 2
How to image stream to jasper report ?

It is very easy to pass image stream to jasper report pdf.

Steps are,

1) Convert the image to stream and set it in parameter ("logo")
2) In jrxml file , create a parameter with parameter class as "java.io.InputStream"
3) Place a image on the jrxml design, set "Image Expression" to $P{logo} and "Expression class" to java.lang.String

    public void createPdfWithImageAsStream() {
        try {
            System.out.println("pdf report creation started .... ");

            String outFileNamePDF = "/Users/user/Tried/POC/imageStreamToPdf/createPdfWithImageAsStream.pdf";
            new File(outFileNamePDF).getParentFile().mkdirs();
            String containerJrxmlFile = "/Users/user/Tried/POC/imageStreamToPdf/createPdfWithImageAsStream.jrxml";
            // pass logo as stream.
            Map<String, Object> parameters = new HashMap<String,Object>();
            File img = new File("/Users/user/Desktop/wallpaper.jpg");
            InputStream fis = new FileInputStream(img);
            parameters.put("logo", fis);
            InputStream reportStream = new FileInputStream(containerJrxmlFile);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(reportStream);
            JasperDesign jasperDesign = JRXmlLoader.load(bufferedInputStream);
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());
            JRExporter exporter = new net.sf.jasperreports.engine.export.JRPdfExporter(); 
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileNamePDF);
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            System.out.println("pdf report created .... ");
        } catch (Exception e) {
  //Snippet 3
  How to integrate Dynamic Image at JasperReport using java
This post is help full for integration dynamic image at jasper when your image data in blob or longblob in database.

there is following step to get your desire output:

Step-1: Make your field  in Jasper through palette:

field Class: java.lang.object

Step-2: Drop image box from palette and provide properties as:

Image Expression  :  net.sf.jasperreports.engine.util.JRImageLoader.loadImage((byte[])$F{logo})
Expression Class    java.awt.Image

Step-3: Java variable
         set logo as java.lang.Object datatype and set image data (byte[]) in logo variable .
               private Object logo;


 Java Code:
public class DemoJasperData{

private Object logo;

public Object getLogo() {
        return logo;

    public void setLogo(byte[] yourImagebyteData) {
        this.logo = yourImagebyteData;


.jrxml :

<field name="logo" class="java.lang.Object"/> 
<image scaleImage="FillFrame">
                <reportElement x="9" y="8" width="100" height="98" uuid="053b21f7-00a3-4387-a6b3-c33475287f4c"/>