为什么字体扩展仅适用于PDF而不适用于其他格式(HTML,XLS,DOC)?

为什么字体扩展仅适用于PDF而不适用于其他格式(HTML,XLS,DOC)?

问题描述:

通过Jaspersoft Studio,我们使用以下设置将用于webapp的内置Windows Calibri字体变体导出到字体扩展JAR中:

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:

导出的jrfontextensions.jar中的目录结构如下:

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

jasperreports_extension.properties的内容:

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

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>

现在,当我们将此报告导出为PDF时,Calibri字体作为Custom-Font嵌入到PDF中按预期正确显示。

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

但是在尝试将报告导出为HTML,DOC和XLS等其他格式时遇到了问题。 Web浏览器或办公应用程序似乎无法识别自定义字体并回退到其默认字体之一:XLS报告默认为DejaVu Sans,而HTML和DOC报告默认为Times New Roman。 PDF,XLS,DOC和HTML中的输出分别如下:

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:

我觉得我们必须遗漏一些明显的东西 - 或许我们缺少一些模糊的JRExporter配置?

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

某些背景

字体通常存在于文档之外(安装在pc上),并非所有文档类型都允许您在文档中嵌入(包含)字体。与您的文档类型相关,可以将字体嵌入到文档中。

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 ,是的,你可以嵌入字体

  • PDF, yes you can embed the font

HTML ,字体无法直接嵌入,但jasper-report将创建
a css包含 @ font-face 的网址,因此如果broswer支持
,则会下载字体

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),将字体嵌入 apache-poi
(jasper报告使用的库)可能是
possibile
但AFIK(99%肯定)jasper-reports没有开发代码尝试和
使用它,因为库不直接支持。

Office (xls,xlsx,doc,docx,ppt), to embed font in apache-poi (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.

你误解了字体映射界面


指定替换字体使用...当不可用

在此界面中,您不应该输入字体名称,字体名称,字体系列如果您的字体不可用,则表示将使用,您需要指定在找不到字体时应使用的其他标准默认字体。

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.

另一个好的设计规则(没有详细记录)是用作姓氏(当您配置字体系列时)字体的确切名称(如果安装),因此如果计算机安装了字体,它将被找到,因为你的名字将映射安装的名称。在您的情况下,您应该将Custom-Font替换为Calibri

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"

为什么我的html无效?,如在 dada67 #comment79881902_46448242>评论如果您想使用自定义,则不应使用字体映射作为html font(映射将重定向到映射的字体),然后搜索css,使用代码,如

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将创建文件 myHtml.html 还有一个名为 myHtml_htmlfiles 的文件夹,在此文件夹中css应该与您的 .ttf 一起出现字体。如果您自然地在服务器上部署它,则需要部署它们。

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.