Reputation: 17009
I read
and I expected that Jasper Reports throws a JRFontNotFoundException
if a font is not installed on the machine.
But my application doesn't throw a JRFontNotFoundException
, although I have not installed the used font "Helvetica" on any (Jaspersoft Studios, JasperReports, Adobe Acrobat Reader) machine.
JRXML:
<parameter name="timestamp" class="java.util.Date"/>
[...]
<textField>
<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"/>
</reportElement>
<box padding="2"/>
<textElement textAlignment="Left" verticalAlignment="Top">
<font size="8" pdfFontName="Helvetica" pdfEncoding="Cp1250" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[DATEFORMAT($P{timestamp},"dd.MM HH:mm")]]></textFieldExpression>
</textField>
Maven dependencies:
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>5.6.0</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-functions</artifactId>
<version>5.6.0</version>
</dependency>
Java:
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 JRFontNotFoundException
is only thrown if font in the attribute fontName
is not installed:
Exception raised when a font name used as value for the
fontName
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 pdfFontName
is not installed (instead of using any other installed font)?
Upvotes: 4
Views: 1977
Reputation: 21710
Your are setting pdfFontName
not fontName
pdfFontName
was an old way, now deprecated to indicate what font the itext library should use, jasper-reports will not throw JRFontNotFoundException
if the font is missing, instead itext will throw an exception that is caught and relaunched as a JRRuntimeException
.
In itext Helvetica is included as afm
file, hence itext will not throw any exception if this is used, however this does not guarantee that your text is rendered correctly if you are indicating another font (in your case not indicating = default font) in jasper-reports. In fact this is a mess and both pdfFontName
and pfdEncoding
was deprecated.
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 insist (for the sake of the question) then also set fontName="Helvetica"
, setting the jasper-reports font will raise a JRFontNotFoundException
if not found.
The correct way is to only set the fontName
and then provide font-extensions, in the font-extension you include the actual ttf
, indicate the encoding and if it should be embedded.
BTW: I would use encoding Identity-H this is recommend for newer PDF standards and gives you the ability to mix different encoding.
Upvotes: 5