通过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?
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 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.
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] 删除。
我来说两句