Image is black when exported to pdf

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.

Martijn
hem's picture
hem
237
Joined: Feb 26 2007 - 4:39pm
Last seen: 6 years 1 month ago

18 Answers:

We're having this same problem (only on Macintosh?)
tracbt's picture
10
Joined: Dec 19 2006 - 6:42am
Last seen: 16 years 9 months ago
I'm using linux.

I don't know how to solve this problem.
hem's picture
hem
237
Joined: Feb 26 2007 - 4:39pm
Last seen: 6 years 1 month ago
I'm using JasperReports on Macintosh since long time, but I've not problem to
display image and export to pdf.
May you tell me more about how you displays the BufferImage.
Do you use JRImageRenderer?
slaisne's picture
473
Joined: Nov 16 2006 - 4:11am
Last seen: 2 years 10 months ago
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.
(java.awt.Image)$F{grafiek}

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.
hem's picture
hem
237
Joined: Feb 26 2007 - 4:39pm
Last seen: 6 years 1 month ago
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.

:cheer:
hem's picture
hem
237
Joined: Feb 26 2007 - 4:39pm
Last seen: 6 years 1 month ago
the emoticons messed up my reply:

TYPE_INT_ARGB
was changed into
TYPE_INT_RGB

problem solved....
hem's picture
hem
237
Joined: Feb 26 2007 - 4:39pm
Last seen: 6 years 1 month ago
Hi,

What version of JasperReports are you using and on what version of JDK?

Thanks,
Teodor
teodord's picture
53592
Joined: Jun 30 2006 - 9:00am
Last seen: 16 hours 4 min ago
Jasperreport 1.3.0
Java 1.6
on Linux and windows 2000
hem's picture
hem
237
Joined: Feb 26 2007 - 4:39pm
Last seen: 6 years 1 month ago
We actually encountered the same problem when moving to 1.3.2 (jdk 6 u1, XP) Is the provided solution the way to go, or does this need to be fixed in Jasperreports?

kind regards,
Christiaan
christiaan_se's picture
Joined: Jul 18 2006 - 11:57pm
Last seen: 8 years 4 months ago
Hi,

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,
Teodor
teodord's picture
53592
Joined: Jun 30 2006 - 9:00am
Last seen: 16 hours 4 min ago
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,
Christiaan
christiaan_se's picture
Joined: Jul 18 2006 - 11:57pm
Last seen: 8 years 4 months ago
2nd attempt adding files [file name=Images_jasper_1.zip size=162589]http://www.jasperforge.org/components/com_joomlaboard/uploaded/files/Ima...
christiaan_se's picture
Joined: Jul 18 2006 - 11:57pm
Last seen: 8 years 4 months ago
christiaan_se's picture
Joined: Jul 18 2006 - 11:57pm
Last seen: 8 years 4 months ago
and the pdf with version 1.3.3 (nice work on the size reduction btw;)

Also, we are using the itext 2.0.2 [file name=Images_jasper_1.pdf size=99732]http://www.jasperforge.org/components/com_joomlaboard/uploaded/files/Ima...
christiaan_se's picture
Joined: Jul 18 2006 - 11:57pm
Last seen: 8 years 4 months ago
Hi,

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.
Teodor
teodord's picture
53592
Joined: Jun 30 2006 - 9:00am
Last seen: 16 hours 4 min ago
Thanks, looks great now.
christiaan_se's picture
Joined: Jul 18 2006 - 11:57pm
Last seen: 8 years 4 months ago
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 NA
naeemahmad's picture
Joined: Jan 26 2009 - 3:26am
Last seen: 14 years 8 months ago

Hi!

I have the same problem but with chart. I am creating a dynamic chart with jrDesignChart.

When I execute it´s ok i can see the chart, but when i print it, I get a black square.

Thanks in advance

nnvc's picture
527
Joined: Jul 31 2009 - 9:57am
Last seen: 14 years 2 months ago
Feedback
randomness