开发者

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.


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


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


In newer versions you need to configure the net.sf.jasperreports.image.dpi property globally by defining a jasperreports.properties file inside WEB-INF/classes/ folder with this line in it:

net.sf.jasperreports.image.dpi=300

A server restart is also needed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜