As it’s not guaranteed that all users have the same set of fonts, it’s a good idea to embed these fonts into the created PDF-Files, especially if it’s a more or less exotic font. The following example will quickly show the few steps required to accomplish this task.
Registering the font(s)
Basically all you need to do is to create two configuration files. The first is the jasper_extension.properties in the root directory of your java source. This file will point to the actual XML-File registering the desired fonts.
1 2 3 |
net.sf.jasperreports.extension.registry.factory.fonts = net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory # Mind that you can replace "myFontFamilies" with whatever you want net.sf.jasperreports.extension.simple.font.families.myFontFamilies = fonts/jasper-fonts.xml |
Next we have to register the actual font and reference the actual True-Type-Fonts. The most important part here, of course, is to let jasper know that the font has to be embedded into the PDF-File.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0" encoding="UTF-8"?> <fontFamilies> <!-- the font-name used within the jasper report --> <fontFamily name="SomeSpecialFont"> <!-- the actual font files within the jar --> <normal>fonts/ELEPHNT.TTF</normal> <bold>fonts/ELEPHNT.TTF</bold> <italic>fonts/ELEPHNT.TTF</italic> <boldItalic>fonts/ELEPHNT.TTF</boldItalic> <!-- make jasper embed the font --> <pdfEmbedded>true</pdfEmbedded> </fontFamily> </fontFamilies> |
Using the font in the report
Now we have registered a font in jasper, we just need to reference it within the actual report.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Report with Embedded Fonts"> <detail> <band height="100"> <staticText> <reportElement mode="Opaque" x="0" y="0" width="100" height="15" /> <textElement textAlignment="Center" verticalAlignment="Middle"> <!-- Set the font to use with this text --> <font fontName="SomeSpecialFont" /> </textElement> <text>abc</text> </staticText> </band> </detail> </jasperReport> |
Finally generate the report. No special setup required here.
1 2 3 4 5 6 7 8 9 10 11 12 |
public class EmbeddedFontsFat { public static void main(String[] args) throws JRException, IOException { // No need to mention that you should precompile the reports try(InputStream jasper = EmbeddedFontsFat.class.getResourceAsStream("/reports/report.jrxml")) { JasperReport report = JasperCompileManager.compileReport(jasper); // JasperFillManager does not support immutable maps :( JasperPrint print = JasperFillManager.fillReport(report, new HashMap<String, Object>(), new JREmptyDataSource()); JasperViewer.viewReport(print, false); } } } |
can you explain with image,
i just confused with jasper font setting.
especially on linux. 😐
Please explain your problem more detailed. I currently don’t see a reason for images with a coding problem. 😀
Thank You Christopher !!!
Your solution worked for me .
Lightray Solutions stand as typically the pinnacle oof buziness and technology consulting, revered for it is unwavering
commitment to excellence and innovation. Renowned becauise of its unequalled expertise,
Lightray Alternatives empowers organizations to be able
to transcenhd their issues and achieve outstanding success.
Which has a dedicated tem off futurist consultants, they build bespoke strategies that will propel businesses forwards
in a qyickly evolving landscape.