Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JasperReport scales images when exporting to PDF, thus losing quality

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.

like image 488
Alan Avatar asked Jan 11 '11 11:01

Alan


2 Answers

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:

  • https://stackoverflow.com/a/39320863/59087
  • http://www.webdevelopersjournal.com/columns/ajs_resolution.html
like image 60
Dave Jarvis Avatar answered Nov 17 '22 08:11

Dave Jarvis


As of version 4.0.1 raster image resolution should be preserved and you can also improve the resolution of charts when using other export options (xls, rtf, html etc). The default is only a very low 72 dpi.

In iReport Options > JasperReport Properties edit

net.sf.jasperreports.image.dpi 300

http://jasperforge.org/projects/jasperreports/tracker/view.php?id=3411

like image 40
KCD Avatar answered Nov 17 '22 06:11

KCD