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)?
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.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.