iReport外部字体 [英] iReport external font
问题描述
我正在尝试在PDF文档中使用外部字体。通过设置 - >字体 - >安装新字体安装后,我在iReport中使用它没有问题。
I am trying to use external font in PDF document. I have no problem with using it in iReport after installing it via Settings -> Fonts -> Install new font.
当我将新字体作为扩展名导出并将该jar添加到java项目的类路径时,无法生成PDF并且
The problem that when I export the new font as extension and adding that jar to java project's classpath - the PDF couldn't be generated and fails with
JRFontNotFoundException:字体'Arial Custom'不可用于JVM异常
我做错了什么?
谢谢
What I am doing wrong? Thank you
UPD#1:
jasperreports_extensions.properties
jasperreports_extensions.properties
net.sf.jasperreports.extension.registry.factory.fonts=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.ireportfamily1329192368547=fonts/fontsfamily1329192368547.xml
fontsfamily1329192368547.xml
fontsfamily1329192368547.xml
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
<fontFamily name="Arial Custom">
<normal><![CDATA[fonts/arial.ttf]]></normal>
<bold><![CDATA[fonts/arialbd.ttf]]></bold>
<italic><![CDATA[fonts/ariali.ttf]]></italic>
<boldItalic><![CDATA[fonts/arialbi.ttf]]></boldItalic>
<pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
<pdfEmbedded><![CDATA[true]]></pdfEmbedded>
<locales>
<locale><![CDATA[en_US]]></locale>
</locales>
</fontFamily>
</fontFamilies>
fonts目录包含所有ttf文件。我刚把那个jar添加到了classpath。
fonts directory includes all ttf files. I just added that jar to classpath.
推荐答案
这是我的工作样本。
字体定义文件(我从字体的jar文件中挖掘出来):
The font definition file (I dig it from the font's jar file):
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
<fontFamily name="Arial">
<normal><![CDATA[fonts/arial.ttf]]></normal>
<bold><![CDATA[fonts/arialbd.ttf]]></bold>
<italic><![CDATA[fonts/ariali.ttf]]></italic>
<boldItalic><![CDATA[fonts/arialbi.ttf]]></boldItalic>
<pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
<pdfEmbedded><![CDATA[false]]></pdfEmbedded>
</fontFamily>
</fontFamilies>
jar文件位于应用程序的类路径中。
The jar file is in the application's classpath.
这是我的java代码:
And here is my java code:
String defaultPDFFont = "Arial";
JRProperties.setProperty("net.sf.jasperreports.awt.ignore.missing.font", "true");
JRProperties.setProperty("net.sf.jasperreports.default.font.name", defaultPDFFont);
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params);
JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);
我设置 net.sf.jasperreports。 awt.ignore.missing.font
属性以防止出现错误( JRFontNotFoundException:字体'Arial Custom'不可用于JVM异常
)以防缺少字体。我设置 net.sf.jasperreports.default.font.name
属性以确定将要使用的字体在生成的pdf文件中使用。
I set the net.sf.jasperreports.awt.ignore.missing.font
property to prevent the error you have (JRFontNotFoundException: Font 'Arial Custom' is not available to the JVM exception
) in case the font is missing. And I set the net.sf.jasperreports.default.font.name
property for determine the font that will be used in the resulting pdf file.
您可以阅读有关此属性的信息: net.sf.jasperreports.awt.ignore.missing.font 和 net.sf.jasperreports.default.font.name 。
You can read info about this properties: net.sf.jasperreports.awt.ignore.missing.font and net.sf.jasperreports.default.font.name.
生成的pdf文件在此示例中使用此字体( Arial
)。
The resulting pdf file is using this font (Arial
in this sample).
这篇关于iReport外部字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!