JasperReport scales images when exporting to PDF, thus losing quality - jasper-reports

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.

Related

Publication ready figures-Matlab [duplicate]

This question already has answers here:
How can I plot professional quality graphs in matlab? [closed]
(2 answers)
Closed 2 years ago.
I want to create high-quality figures ready to be submitted for publication. What is the best font size, type of file to be saved as(.fig, .eps, .png?) and generally the characteristics required for a top-quality figure?
Depends on the paper editing software you use.
If you use Latex or Overleaf, exporting to PDF is the easiest/simplest/smallest-file-size one while keeping image quality as high as possible (i.e. vector format). My typical workflow is
create the plot, set font size to 18 o 20 use set(gca,'fontsize',..).
set(gcf,'paperpositionmode','auto'); this is important! it makes your figure what-you-see-is-what-you-get, so you can adjust figure sizes relative to the font size.
save as PDF or print -dpdf to export to the figure.
call pdfcrop yourfigure.pdf to remove the margins, pdfcrop is available on Linux/Mac, but also available on Windows. If you can't install pdfcrop, you can also use inkscape to fit plot to page.
I usually try to make the figure in its final look using matlab commands (so it can be easily reproduced), but in case there are changes that I can;t make automatically, I will open the pdf using inkscape and manually edit.
if the figure is a 3D surface/mesh rendering with transparency, I will directly call print -dpng -r300 myfile.png to create a png bitmap image directly. Exporting to pdf generates huge file sizes and slow rendering.
once done, upload the pdf or png to overleaf, the platform accepts these formats directly.
If using older versions of MS Word, I had been exporting images to EPS and insert to keep the images in highest quality possible. However, since 2017, this feature was turned off
https://support.microsoft.com/en-gb/office/support-for-eps-images-has-been-turned-off-in-office-a069d664-4bcf-415e-a1b5-cbb0c334a840
but one can still modify a registry key to turn it on.
Later MS Office accepts svg, which you can export from matlab.

Matlab figure size formatting for Word

I'm trying to create MATLAB figures to put into a paper. The paper has very specific sizing instructions for figures that I'm having trouble matching in MATLAB. The figures need to be no greater than 3.5" width, >300 DPI, with 8pt font.
In my code, I use the following to try to set the parameters:
set(gcf,'PaperUnits','inches');
set(gcf,'PaperPosition',[0 0 3.5 3.5]);
xlabel('x-axis label','FontSize',8);ylabel('y-axis label','FontSize',8);
set(gca,'FontSize',8);
print('-djpeg','-r300','filename.jpg')
This should be giving me a 300 DPI, 3.5"x3.5" JPEG image with an 8pt font size. However, when I import the image into Word, it becomes 6.5" x 6.5" and the font size is larger than Word's 8pt font. Even if I resize the image, the font size is still too large, though it should maintain the same DPI. Are the FontSize and PaperPosition parameters not working as I expect they should or is Word doing something strange for importing?
The font size issue was caused due to differing fonts used in MATLAB and Word. Once I learned about set(gca,'FontName'), the font size seemed to be correct when the image was manually resized to 3.5" x 3.5".
The image size issue seemed to be related to saving it as a JPEG. Once I swapped to PNG, the image was the correct size by default. Looking into the JPEG properties, it had the correct number of pixels for a DPI of 300 at 3.5", the sole issue was that it would have to be manually resized. Thanks for the comments that led me to finding a solution.

Cant export matlab figure using custom font

I'm trying to export my plot with Tahoma font :
However I always end up with the following figure :
Which is not the font I chose !! can someone tell me why is this happening ?
Also, the legend's font do not change either.
For export figures in MATLAB, I highly recommend you to try export_fig. WYSIWYG!
This function saves a figure or single axes to one or more vector and/or bitmap file formats, and/or outputs a rasterized version to the workspace, with the following properties:
Figure/axes reproduced as it appears on screen
Cropped borders (optional)
Embedded fonts (pdf only)
Improved line and grid line styles
Anti-aliased graphics (bitmap formats)
Render images at native resolution (optional for bitmap formats)
Transparent background supported (pdf, eps, png)
Semi-transparent patch objects supported (png only)
RGB, CMYK or grayscale output (CMYK only with pdf, eps, tiff)
Variable image compression, including lossless (pdf, eps, jpg)
Optionally append to file (pdf, tiff)
Vector formats: pdf, eps
Bitmap formats: png, tiff, jpg, bmp, export to workspace
This function is especially suited to exporting figures for use in publications and presentations, because of the high quality and portability of media produced.
I think you cannot embed fonts to MATLAB figures normally, you can just use Type1 fonts.
Check this MATLAB function, by Oliver Woodford, maybe it can help: [Link]

How to improve the quality (smoothness) of lines in contourplots Matlab's

How the line smoothness in a contour plot can be improved for publications? For instance, the dotted lines look really bad, the continuous lines look as if their thickness varies. See below
Here's part of the code:
Vals = [0:5:200]; contourf(X,Y,W,Vals,'EdgeColor','k','LineWidth',1.2,'LineStyle',':');axis square;grid;hold on
Vals = [10:10:200]; contour(X,Y,W,Vals,'EdgeColor','k','LineWidth',1.2);
Vals = [20 : 20 : 200]; [C,h] = contour(X,Y,W,Vals,'Color','k','LineWidth',1.8);
clabel(C,h,'FontName','Palatino Linotype','FontAngle','italic','Fontsize',9,'Color','w')
print -djpeg -r300 filename
Thanks!
Saved as png doesn't help much... check the lines :/ See below:
Check the dotted lines now...
Here's saving as eps (-r1200)... it looks better
Exporting as vector graphics will definitely improve the image over what you see on your screen; I use LaTeX for publications and you can either export to eps for postscript output, and use epstopdf for PDF output, and embed these directly in your document; that would be the best solution.
Additionally, there are also a bunch of general utilities for making your plots look better for camera-ready publications, the most notable that comes to mind is exportfig, which has a load of features to help even with pixel graphics. These go above and beyond just generating smoother-looking images.
http://www.mathworks.us/matlabcentral/fileexchange/23629-exportfig
(copied from that page):
This function saves a figure or single axes to one or more vector and/or bitmap file formats, and/or outputs a rasterized version to the workspace, with the following properties:
Figure/axes reproduced as it appears on screen
Cropped borders (optional)
Embedded fonts (pdf only)
Improved line and grid line styles
Anti-aliased graphics (bitmap formats)
Render images at native resolution (optional for bitmap formats)
Transparent background supported (pdf, eps, png)
Semi-transparent patch objects supported (png only)
RGB, CMYK or grayscale output (CMYK only with pdf, eps, tiff)
Variable image compression, including lossless (pdf, eps, jpg)
Optionally append to file (pdf, tiff)
Vector formats: pdf, eps
Bitmap formats: png, tiff, jpg, bmp, export to workspace
This function is especially suited to exporting figures for use in publications and presentations, because of the high quality and portability of media produced.
Update: I see your example code now. Did you try changing -r300 to some really high value? More pixels per inch should make everything look smoother. For publication, crank it up really high, like -r1200.
Original:
One thing you can try is exporting the plot in some format that supports vector graphics. Matlab supports both PDF and EMF, so try one of those. Export using the saveas command or from the figure's "File -> Save as" menu item. After that, open or import the image file in some other application and hopefully it will look better.
Please add a new screenshot if you get a nicer image!

Matlab: How to save plots of patches / fill() automatically in the code?

quick question: I'm creating "random" polygons using either the patch() or the fill() function in Matlab. This works quite good and it is plotted correctly.
However, I need to at least save a few hundres polygons as images to my hard drive for working with them later - so I'm looking for a way to directly save the image in my function rather than saving each polygon myself using the file-menu.
Is there any way to do this?
Thanks in advance!
You can indeed use the print function, but I would not use the jpeg device. JPEG is never the right format for plots (you will get a lot of artifacts near all your lines).
If you need a bitmap image, try the png or tiff device. If you don't need a bitmap, use the appropriate vector image format: fig is the native MATLAB format (which allows you to edit the plot afterwards), so this is the best one if you stick with MATLAB for all your operations. For exporting to other software, I would recommend pdf (works almost anywhere), epsc (EPS with color, great for LaTeX or inkscape), wmf/emf (Windows Metafile, so Windows only, but great for including the images in MS Office). Or you could of course use any of the other formats mentioned in the print documentation.
Sometimes it's a pain in the neck to get the format of your image all right (especially with PDF output). Just take a look at the different properties of your figure and more specifically the PaperSize, PaperUnits and PaperPosition.
The easiest way, and I guess the best solution, is to save as a .fig file. You can do this by using saveas:
h = figure;
% your plot commands here
saveas(h,'mFile.fig');
Afterwards, you can reload the image with the openfig function:
openfig('mFile.fig');
Have to add this answer. This function is helping a lot.
This function saves a figure or single axes to one or more vector and/or bitmap file formats, and/or outputs a rasterized version to the workspace, with the following properties:
   - Figure/axes reproduced as it appears on screen
   - Cropped/padded borders (optional)
   - Embedded fonts (pdf only)
   - Improved line and grid line styles
   - Anti-aliased graphics (bitmap formats)
   - Render images at native resolution (optional for bitmap formats)
   - Transparent background supported (pdf, eps, png)
   - Semi-transparent patch objects supported (png only)
   - RGB, CMYK or grayscale output (CMYK only with pdf, eps, tiff)
   - Variable image compression, including lossless (pdf, eps, jpg)
   - Optionally append to file (pdf, tiff)
   - Vector formats: pdf, eps
   - Bitmap formats: png, tiff, jpg, bmp, export to workspace