JasperReport scales images when exporting to PDF, thus losing quality

Alan picture Alan · Jan 11, 2011 · Viewed 16.7k times · Source

I have a report designed with iReport 1.3.0. This report includes several images, which are loaded dynamically. Then I use JasperRunManager.runReportToPdfFile to create the PDF. Everything in the PDF looks fine except for the images, which appear scaled.

Can anyone tell me what am I doing wrong?

Thanks.

Answer

Dave Jarvis picture Dave Jarvis · Jan 13, 2011

About Images

If you save the report as HTML, you will notice that the logo appears correctly.

Image files such as PNG are raster images: stored as individual pixels.

Whereas PDF files are primarily vectorized: the elements inside are stored as descriptions of how to draw them. This allows PDFs to scale and be legible at any size.

Using a raster image in a vector format will likely result in a pixelated effect.

Possible Solutions

You have a few options to make the images match the quality of the text, in order of ease:

  • Create a version of the logo at 1200 dpi, scaled 400% larger.
  • Create an SVG version of the logo.
  • Convert the image to a vector format.
  • Scale image 300% and change resolution to 288 dpi.

1200 DPI Version

The image will look sharp until around 400% zoom.

SVG Version

The image will look sharp at every resolution. Replace the normal <image...> XML with the following code (be sure to adjust the width and height accordingly):

<image hAlign="Center" vAlign="Middle">
 <reportElement x="0" y="0" width="179" height="66"/>
 <imageExpression class="net.sf.jasperreports.engine.JRRenderable"><![CDATA[net.sf.jasperreports.renderers.BatikRenderer.getInstance(new java.io.File("/path/to/logo.svg"))]]></imageExpression>
</image>

Convert GIF to SVG

To convert a GIF to SVG, first try a quick web-based tool, such as: http://vectormagic.com

Once converted to SVG, you will have to use the code listed above to display the logo.

Resolution and Scale

See: