Why is font extension working only for PDF but not for other formats (HTML, XLS, DOC)?

2k Views Asked by At

Through Jaspersoft Studio we exported the built-in Windows Calibri font variants for use in a webapp into a font extension JAR, using the following settings:

Font Settings

The directory structure within the exported jrfontextensions.jar is as follows:

--jrfontextensions
  --jasperreports_extension.properties
  --fonts
    --fontsfamily1505796949749.xml
    --Custom_Font
      --calibri.ttf
      --calibrib.ttf
      --calibrii.ttf
      --calibriz.ttf

Contents of jasperreports_extension.properties:

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

Contents of fontsfamily1505796949749.xml:

<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
    <fontFamily name="Custom-Font">
        <normal><![CDATA[fonts/Custom_Font/calibri.ttf]]></normal>
        <bold><![CDATA[fonts/Custom_Font/calibrib.ttf]]></bold>
        <italic><![CDATA[fonts/Custom_Font/calibrii.ttf]]></italic>
        <boldItalic><![CDATA[fonts/Custom_Font/calibriz.ttf]]></boldItalic>
        <pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
        <pdfEmbedded><![CDATA[true]]></pdfEmbedded>
        <exportFonts>
            <export key="html"><![CDATA[Custom-Font]]></export>
            <export key="rtf"><![CDATA[Custom-Font]]></export>
            <export key="xhtml"><![CDATA[Custom-Font]]></export>
        </exportFonts>
    </fontFamily>
</fontFamilies>

Now when we export this report as PDF, the Calibri font embedded into the PDF as the Custom-Font displays correctly, as expected.

But we ran into problems when trying to export the report to other formats like HTML, DOC, and XLS. It seems the web browser or office apps don't recognize the "Custom-Font" and falls back to one of their default fonts: the XLS report defaults to DejaVu Sans, while the HTML, and DOC reports default to Times New Roman. The outputs in PDF, XLS, DOC, and HTML respectively are as follows:

Document output

I feel like we must be missing something obvious here - perhaps we're lacking some obscure JRExporter configuration?

1

There are 1 best solutions below

6
On BEST ANSWER

Some background

Fonts normally exists outside of the document (installed on pc) and not all document types will allow you to embed (include) font in the document itself. Related to your document types this is the possibility to embed fonts into the document.

  • PDF, yes you can embed the font

  • HTML, font can not be directly embedded but jasper-report will create a css that contains @font-face with url, hence if broswer supports it, font will be downloaded

  • Office (xls,xlsx,doc,docx,ppt), to embed font in (library used by jasper report) is probably possibile but AFIK (99% sure) jasper-reports have not developed code to try and use this, since not directly supported by the library.

What should I do now

You are misunderstanding the font-mapping interface

Specify replacement fonts to use ... when not available

In this interface you should not put the name of your font, the font name, font-family you indicate will be used when your font is not available, you need to specify other standard default fonts that should be used if your font is not found.

Another good design rule (not well-documented) is to use as "Family name" (when you configure the font family) the exact name of font (if installed), hence if the computer has the font installed it will be found, since your name will map the name installed. In your case it looks like that you should replace "Custom-Font" with "Calibri"

Why is my html not working?, as indicated in comment by dada67 you should not use font-mapping for html if you like to use custom font (the mapping will redirect to mapped font), then search for the css, using code like

JasperReport report = JasperCompileManager.compileReport("jasper/FontExportTest.jrxml");
JasperPrint jasperPrint = JasperFillManager.fillReport(report, new HashMap<String, Object>(), new JREmptyDataSource());
        
HtmlExporter exporter = new HtmlExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleHtmlExporterOutput(new File("myHtml.html")));
SimpleHtmlExporterConfiguration configuration = new SimpleHtmlExporterConfiguration();
exporter.setConfiguration(configuration);
exporter.exportReport();

JasperReport will create both the file myHtml.html but also a folder with the name myHtml_htmlfiles, in this folder the css should be present, together with your .ttf font. If you deploy it on server naturally you need to deploy both.