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

JA-索利文

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

字型设定

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

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

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的内容:

<?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时,将按预期正确显示作为自定义字体嵌入到PDF中的Calibri字体。

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:

文件输出

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

Petter Friberg

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"

为什么我的HTML不起作用?,如表示意见dada67你不应该使用字体映射为HTML,如果你喜欢使用自定义的字体(映射将重定向到映射字体),然后搜索CSS,使用类似的代码

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字体。如果自然将其部署在服务器上,则需要同时部署两者。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

悬停格式仅适用于 $ 符号,不适用于其他货币?

分割多页PDF的Python脚本仅适用于某些PDF,而不适用于其他PDF

NPOI 不适用于 .xls 格式

别名导入不适用于.jsx扩展名

字体家族不适用于PDF

背景图片仅适用于首页,而不适用于其他图片

.htaccess重定向仅适用于某些页面,而不适用于其他页面

Codeigniter文件上传许可仅适用于图像,不适用于其他图像

为什么此正则表达式替换不适用于JavaScript,而仅适用于其他引擎?

为什么动画仅适用于第一张幻灯片而不适用于其他幻灯片?

为什么Mono 3.x特别适用于OpenSuse,而不适用于其他Linux(例如Ubuntu)

Javascript 不适用于 Internet Explorer,但适用于所有其他浏览器。为什么?

ASP.NET MVC路由不适用于.html扩展名

Visual Studio Angular-扩展名似乎不适用于.html文件

为什么django的prefetch_related()仅适用于all()而不适用于filter()?

为什么图例选择仅适用于“ ax.twinx()”而不适用于“ ax”?

为什么Multi Peer Connectivity仅适用于MCAdvertiserAssistant而不适用于MCNearbyServiceAdvertiser

为什么HTML中“ <input>”标记中的“ size”属性仅适用于TEXT而不适用于NUMBER?

为什么bash brace扩展不适用于命令?

为什么 CSS 规则不适用于真正的 HTML 页面,但适用于 JSFiddle?

为什么 Spock 的交互不适用于其他类构建的 Mock

CakePHP路由器仅适用于'/'其他,为什么?

javascript效果仅适用于第一个元素,而不适用于其他元素?

渐变仅适用于一张照片,而不适用于其他照片

POST方法/创建操作仅适用于某些方法,而不适用于其他方法,Feedzirra

PHP jQuery Ajax呼叫仅适用于Chrome,不适用于其他浏览器

CSS代码仅适用于一部分,而不适用于其他部分

Chrome 仅适用于 1 个帐户,但不适用于其他 3 个帐户

为什么提交输入样式既不适用于Safari(Mac)也不适用于Chrome,而仅适用于背景颜色?