dur dur - 1 year ago 207
Java Question

Why doesn't jasper report throw a JRFontNotFoundException if font for PDF is missing?

I read

and I expected that Jasper Reports throws a
if a font is not installed on the machine.

But my application doesn't throw a
, although I have not installed the used font "Helvetica" on any (Jaspersoft Studios, JasperReports, Adobe Acrobat Reader) machine.


<parameter name="timestamp" class="java.util.Date"/>
<reportElement x="0" y="0" width="50" height="16" uuid="0007846a-26f1-457a-a198-67a2f7c8417c">
<property name="local_mesure_unitwidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<property name="local_mesure_unitx" value="pixel"/>
<property name="com.jaspersoft.studio.unit.x" value="px"/>
<property name="local_mesure_unity" value="pixel"/>
<property name="com.jaspersoft.studio.unit.y" value="px"/>
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<box padding="2"/>
<textElement textAlignment="Left" verticalAlignment="Top">
<font size="8" pdfFontName="Helvetica" pdfEncoding="Cp1250" isPdfEmbedded="true"/>
<textFieldExpression><![CDATA[DATEFORMAT($P{timestamp},"dd.MM HH:mm")]]></textFieldExpression>

Maven dependencies:



private byte[] createPdf() {

try {
InputStream is = getClass().getResourceAsStream("MyReport.jasper");
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(is);
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("timestamp", new Date());
JRDataSource jrDataSource = new JRBeanCollectionDataSource(new Vector(), false);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, jrDataSource);
byte[] pdf = JasperExportManager.exportReportToPdf(jasperPrint);
return pdf;
} catch (JRException e) {
throw new RuntimeException("Could not create PDF.", e);

The reason is that
is only thrown if font in the attribute
is not installed:

Exception raised when a font name used as value for the
attribute in the report template, is not found in any of the runtime available JasperReports font extensions, nor among the font names available to the Java Virtual Machine.

Is there any way to abort generation of PDF if the font in attribute
is not installed (instead of using any other installed font)?

Answer Source

Your are setting pdfFontName not fontName

The pdfFontName is the name of an equivalent PDF font required by the iText library and I do not think that jasper-reports check if this font is available in in the GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(), it simple creates a itext object with this font and let itext throw error if it can not.

Note: it do check if the fontName is available in font-extension xml (in this case it will ignore pdfFontName)

In itext Helvetica is included as AFM file (only font-metrics), not the actual font due to licensing problems see http://stackoverflow.com/a/28104677/5292302

This is a mess and jasper-reports deprecated pdfFontName and pdfEncoding

The correct way is to set the fontName and then provide font-extensions, in the font-extension you provide the actual ttf, indicate the encoding and if it is embedded.

As for your question: Is there any way to abort generation of PDF if the font in attribute pdfFontName is not installed?

Don't use pdfFontName, but if you are really stubborn then also set fontName="Helvetica", hence also set the jasper-reports font.

BTW: I would use encoding Identity-H this is recommend for newer PDF standards and gives you the ability to mix different encoding.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download