flyingfox
flyingfox

Reputation: 13506

How to show an image from an image inputstream in Birt?

I am using to develop some reports,I want to display some pie charts in the pdf report,I found the birt chart function is not very flexiable,so I want to use to generate an image and show it in the report instead. Now I have a question: can we show image in with the image inputstream,so that the image do not need to be generated and store in some place? I know I can generate the image into a jpg file and store it in some place ,then I can access it birt report via the location of this image file.But if I do as that ,each time when we view the birt report ,it will generate a new image file,I do not want to generate so much files.

Any good ideas?

Upvotes: 0

Views: 590

Answers (1)

hvb
hvb

Reputation: 2669

There are two possible solutions.

As you said, you can save the charts as files. I do this in a report where I generate dozens of control charts (German: "Regelkarten"). To tidy up after the report has finished, I add the generated file names to a list, then I delete the files in the list in the report's afterRender script like this.

var images = vars["images"];
for (var i=0; i<images.size(); i++) {
    var fname = images.get(i);
    if (new java.io.File(fname)['delete']()) {
        log.debug("Temp. image file removed: " + fname);
    } else {
        log.error("Could not delete temp. image file " + fname);
    }
}

Note: This is OK for generating PDF or Word, but probably it wouldn't work for HTML.

In my case, I did this because I could check the generated charts outside of BIRT by just commenting out the clean up routine.

The other option is to generate a byte[]. BIRT complains in the log file then (Rhino, something about unknown type mapping Java<->Javascript), but it works. I never have used this with JFreeChart, but that's how we do it for generating DataMatrix ("2D-Barcode") with an ancient commercial Java library from a company called "ID Automation".

The image item references row["dataMatrix_AuftID"], which is defined as type: Java Object, expression: barcode.dataMatrix(row["AUFT_ID"],70,70,80,0.03)

The function barcode.dataMatrix in our Javascript wrapper library for the Java library calls the Java library. Its last few lines look like this, which should give you an idea for your case:

    var image = new java.awt.image.BufferedImage(siz.width, siz.height, t);

    // get graphic context of image
    var imgGraphics = image.createGraphics();

    // paint DataMatrix in graphics context of image
    bc.paint(imgGraphics);

    // get the image's bytes in PNG format
    var baos = new java.io.ByteArrayOutputStream();
    javax.imageio.ImageIO.write(image, "png", baos);
    var bytes = baos.toByteArray();

    return bytes;

As a side-note, for a proof of concept I showed that it is also possible to generate SVG in memory and use it with BIRT. Never used this in production, however.

Upvotes: 1

Related Questions