Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iReport external font

I am trying to use external font in PDF document. I have no problem with using it in iReport after installing it via Settings -> Fonts -> Install new font.

The problem that when I export the new font as extension and adding that jar to java project's classpath - the PDF couldn't be generated and fails with

JRFontNotFoundException: Font 'Arial Custom' is not available to the JVM exception

What I am doing wrong? Thank you

UPD#1:

jasperreports_extensions.properties

net.sf.jasperreports.extension.registry.factory.fonts=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.ireportfamily1329192368547=fonts/fontsfamily1329192368547.xml

fontsfamily1329192368547.xml

<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>

   <fontFamily name="Arial Custom">
       <normal><![CDATA[fonts/arial.ttf]]></normal>
       <bold><![CDATA[fonts/arialbd.ttf]]></bold>
       <italic><![CDATA[fonts/ariali.ttf]]></italic>
       <boldItalic><![CDATA[fonts/arialbi.ttf]]></boldItalic>
       <pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
       <pdfEmbedded><![CDATA[true]]></pdfEmbedded>
       <locales>
               <locale><![CDATA[en_US]]></locale>
       </locales>
   </fontFamily>    

</fontFamilies>

fonts directory includes all ttf files. I just added that jar to classpath.

like image 964
nKognito Avatar asked Feb 13 '12 05:02

nKognito


1 Answers

Here is my working sample.

The font definition file (I dig it from the font's jar file):

<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
   <fontFamily name="Arial">
       <normal><![CDATA[fonts/arial.ttf]]></normal>
       <bold><![CDATA[fonts/arialbd.ttf]]></bold>
       <italic><![CDATA[fonts/ariali.ttf]]></italic>
       <boldItalic><![CDATA[fonts/arialbi.ttf]]></boldItalic>
       <pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
       <pdfEmbedded><![CDATA[false]]></pdfEmbedded>
   </fontFamily>
</fontFamilies>

The jar file is in the application's classpath.

And here is my java code:

String defaultPDFFont = "Arial";

JRProperties.setProperty("net.sf.jasperreports.awt.ignore.missing.font", "true");
JRProperties.setProperty("net.sf.jasperreports.default.font.name", defaultPDFFont);

JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params);

JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);

I set the net.sf.jasperreports.awt.ignore.missing.font property to prevent the error you have (JRFontNotFoundException: Font 'Arial Custom' is not available to the JVM exception) in case the font is missing. And I set the net.sf.jasperreports.default.font.name property for determine the font that will be used in the resulting pdf file.

You can read info about this properties: net.sf.jasperreports.awt.ignore.missing.font and net.sf.jasperreports.default.font.name.

The resulting pdf file is using this font (Arial in this sample).

like image 155
Alex K Avatar answered Sep 18 '22 13:09

Alex K