Jump to content
Changes to the Jaspersoft community edition download ×

Image is black when exported to pdf


Recommended Posts

I put an JRBeanCollectionDatasource in

a jasperreport.

The Bean contains a BufferedImage. this image is

placed on the report.

When I show the Report with Jasperviewer the Image is

Ok, but when the report is exported to Pdf the image becomes black.

I also tried to export the report to HTML, but then the image becomes blurry.


I exported the report with:


JasperExportManager.exportReportToPdfFile(jasperPrint, "/test.pdf");


Please help.



Link to comment
Share on other sites

  • Replies 19
  • Created
  • Last Reply

Top Posters In This Topic

The bufferedimage is created from a graphics2D.

The image is placed in a JRBeanCollectionDatasource

as an Object.

This field is placed on the report as an image.



Do I have to convert this image to a JRImageRenderer?

and how do I do that?


Still it's strange that the image appears correct in

Jasperviewer and not in the pdf.

It seams like the pdfconvertor does't work as it should.

Link to comment
Share on other sites

  • 4 weeks later...

I found the problem.


The Bufferedimage was created by:

BufferedImage image = new BufferedImage(X, Y, BufferedImage.TYPE_INT_ARGB);


I changed it to:

BufferedImage image = new BufferedImage(X, Y, BufferedImage.TYPE_INT_RGB);


Now the image appears in the created pdf file.


I'm curious why it doesn't work with the first option,

so if someone knows the reason, please let me know.



Link to comment
Share on other sites

  • 4 weeks later...



In JasperReports 1.3.0 we introduced some modifications to better deal with image loading and encoding as well as transparent images.

If you experience problems with images that used to work when upgrading to a more recent JR version, then we need to know what kind of images are they and how you create them, because they probably worked before due to something that we did wrong in JR and now that we no longer do it, they no longer work as they used to.


Thank you,


Link to comment
Share on other sites

Hi Teodor,

we replaced our BufferedImage.TYPE_INT_ARGB code with ImageIO.read(imageFile) which already gave better transparency results in previewing the report.


When exporting to pdf, the results are different (less good) as the 1.2.6 version. I've attached the pdf documents and the images used.


We actually always convert the images to png before displaying them (could be another format if you think that is more appropriate). So what we basically do:

- load the image with:

BufferedImage bufImage = ImageIO.read(imageFile);

- store the image as bytearray in png format:

ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);

ImageIO.write(image, PICTURE_FORMAT, baos);

- load the image bytes into BufferedImage again and provide it as a parameter to report:

BufferedImage im = ImageIO.read(new ByteArrayInputStream(imagebytes));


kind regards,


Link to comment
Share on other sites



You should not give JasperReports the image as a java.awt.Image instance (or BufferedImage as you say).

If you do so, information about the type of the image is lost and JR would try to encode it as a JPG.

Your images have transparency information in them, because they are PNGs and when embedded as JPGs in PDF, this is how they look.


You should simply pass the image file name to JasperReports and let it load the image. It will be able to detect it is a PNG and transparency information will be preserved.


But if you still think you need to pass them to JasperReports as Image objects (maybe you do some image processing with them), then in the <imageExpression> in JasperReports, wrap the Image object into a JRRenderable instance by calling


JRImageRenderer.getInstance(yourImage, JRRenderable.IMAGE_TYPE_PNG, JRImage.ON_ERROR_TYPE_ERROR)


This way JR will know it is a PNG and will use the appropriate image encoder, and not the default JPG one.


I hope this helps.


Link to comment
Share on other sites

  • 1 year later...

Hi Teodord,

I am using jasperreports 3.0.0 with java 1.6. I am facing the same problem of black image when exporting to pdf. I have tried to wrap my image object into JRRenderable instance but the results are same.

Actually i am creating JfreeChart and creating image through this chart, then i send this image towards jasper to display it into reports. So according to Product requirements i cant save this image and send string path to jasper.

Please give any idea regarding this matter.

Thank you



Link to comment
Share on other sites

  • 8 months later...
  • 14 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...