Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSRS can't properly render *some* images within PDF

I have a report that renders images (jpg) that have been collected from various sources. This works fine within the report viewer, and when exporting via Excel.

However, when exporting to PDF, about 5% of the images are rendered incorrectly as can be seen below, with the original on the left, and what is rendered on the right;

enter image description hererendered image image

I find that if I open up one of these images in mspaint, and just click save, on the next report-run the image is now rendered correctly.

Are there any rules as to what image properties/format are valid for SSRS to render the image correctly within a PDF? Essentially I'd like to somehow find these images that will render incorrectly before the report is run and fix them prior...

like image 515
Stafford Williams Avatar asked Mar 27 '13 08:03

Stafford Williams


3 Answers

Current Workaround

I never ended up getting SSRS to display the the problem images as they were, however, determining before running the report which images would be included in the non-displayable set so they could be converted to a supported format (automatically) was also a solution.

In my case, all images were supplied via users uploading to a website, so I was able to identify and convert images as they arrived. For all existing images, I was able to run a script that identified the problem images and convert them.

Identifying problem images

From the thousands of images I had, I was able to determine that the images that wouldn't render correctly had the following properties:

  1. Image had CMYK colorspace or;
  2. Image had extended color profiles or;
  3. Both of the above

Converting an image

I was originally using the standard .NET GDI (System.Drawing) to manipulate images however the API is often prone to crashes (OutOfMemoryException) when dealing with images that have extra data. As such, I switched to using ImageMagick where for each of the identified images I:

  1. Stripped the color profiles and;
  2. Converted to RGB

Note that the conversion to RGB from CMYK without stripping the color profiles was not enough to get all images to render properly.

I ended up just doing those items on every image byte stream I received from users (without first identifying the problem) before saving an uploaded image to disk. After which, I never had the rendering problem again.

like image 81
Stafford Williams Avatar answered Oct 24 '22 09:10

Stafford Williams


Because of the way the output looks I would say those JPEG images have CMYK colorspace but the SSRS assumes they use RGB colorspace and sets the wrong colorspace in PDF.
If you can post a JPEG image and a sample PDF I can give you more details.

like image 25
Mihai Iancu Avatar answered Oct 24 '22 08:10

Mihai Iancu


I've had exactly the same problem with an image rendering correctly on screen but appearing like the one in the question when I exported the report to PDF. Here's how I solved it.

The Problem

The first clue was this article I came across on MSDN. It seems that regardless of the original image density, the PDF renderer in SSRS resizes all images to 96 DPI. If the original size of the image is larger than the size of the page (or container), then you will get this problem.

The Solution

The solution is to resize the source image such that it will fit on your page. The requires a little calculation depending on your page size and margin settings.

In my case, I'm using A4 paper size, which is 21cm by 29.7cm. However, my left margin is 1.5cm, and my right margin is 0.5cm, for a total inner width of 19cm. I allow an extra 0.5 cm as a margin of error, so I use an inner width of 18.5cm.

21 cm - 1.5 cm - 0.5 cm - 0.5 cm = 18.5 cm

As noted before, the resolution generated by the PDF renderer is 96 DPI (dots per inch). For those of us not in the United States or Republic of Liberia, that's 37.79 DPC (dots per centimetre). So, to get our width:

18.5 cm * 37.79 dpc = 699 pixels

Your result may be different depending on (1) the paper size you are using, and (2) the left and right margins.

As the page is higher than it is wide, we need only resize the width while keeping the image proportional. If you're using a paper size which is wider than it is tall, you'd use the length instead.

So now open the source image in Paint (or your image editor of choice), and proportionally resize the image to the desired width (or length) in pixels, save it, import it into your container, and size the image visually with respect to the container. It should look the same on screen, and now render correctly to PDF.

like image 2
Mark Micallef Avatar answered Oct 24 '22 09:10

Mark Micallef