Java JasperReports 5.6:无法加载以下字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25548404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:37:40  来源:igfitidea点击:

JasperReports 5.6: Could not load the following font

javafontsjasper-reports

提问by Tunguska

I am facing the problem, that JasperReportsstill cannot find the Arial font.

我面临的问题是JasperReports仍然找不到 Arial 字体。

I created a simple Maven Project with following structure and included it to my main application. So the main application contains the installed JAR in classpath:

我创建了一个具有以下结构的简单 Maven 项目,并将其包含在我的主应用程序中。所以主应用程序在类路径中包含已安装的 JAR:

- jasperreports_extension.properties
- fonts
  |-> arial
    |-> ariali.ttf
    |-> arialbi.ttf
    |-> arialbd.ttf
    |-> arial.ttf
  |-> fonts.xml

But I still see following Exception while exporting report to PDF.

但是在将报告导出为 PDF 时,我仍然看到以下异常。

net.sf.jasperreports.engine.JRRuntimeException: Could not load the following font : 
pdfFontName   : Arial
pdfEncoding   : Identity-H
isPdfEmbedded : true

jasperreports_extension.properties

jasperreports_extension.properties

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

fonts.xml:

字体.xml:

<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
    <fontFamily name="Arial">
        <normal>fonts/arial/arial.ttf</normal>
        <bold>fonts/arial/arialbd.ttf</bold>
        <italic>fonts/arial/ariali.ttf</italic>
        <boldItalic>fonts/arial/arialbi.ttf</boldItalic>
        <pdfEncoding>Identity-H</pdfEncoding>
        <pdfEmbedded>true</pdfEmbedded>
    </fontFamily>
</fontFamilies>

Template

模板

<font fontName="Arial" size="8" pdfFontName="Arial" pdfEncoding="Identity-H" isPdfEmbedded="true"/>

回答by Tunguska

The problem was the template itself: At one font-tag the <fontName>property was missing:

问题是模板本身:在一个字体标签中,该<fontName>属性丢失了:

Does not work:

不起作用:

<font size="12" isBold="true" pdfFontName="Arial"/>

Does work:

是否有效:

<font fontName="Arial" size="12" isBold="true" pdfFontName="Arial" pdfEncoding="Identity-H" isPdfEmbedded="true"/>

回答by StarCrafter

Another SolutionWorked For Me is:

另一个对我有用的解决方案是:

JRProperties.setProperty("net.sf.jasperreports.default.pdf.font.name", "Helvetica"); JRProperties.setProperty("net.sf.jasperreports.default.pdf.encoding", "UTF-8"); JRProperties.setProperty("net.sf.jasperreports.default.pdf.embedded", "true");

JRProperties.setProperty("net.sf.jasperreports.default.pdf.font.name", "Helvetica"); JRProperties.setProperty("net.sf.jasperreports.default.pdf.encoding", "UTF-8"); JRProperties.setProperty("net.sf.jasperreports.default.pdf.embedded", "true");

If you set parameters from java side, you have to specify font.name and Helveticasaves the day. I tried Arial here but threw the same error.

如果您从 java 端设置参数,则必须指定 font.name 并且Helvetica可以节省一天的时间。我在这里尝试了 Arial,但抛出了同样的错误。

回答by atiruz

Copy the fonts files (*.ttf)into the folder:

将字体文件(*.ttf)复制到文件夹中:

/path/to/app/WEB-INF/classes/***HERE***

You app will get the fonts from here :)

您的应用程序将从这里获取字体:)

回答by Thomas.be

You must give the correct name of font for the PDF exporter. Your fonts are presents into the embedded fonts jar. You don't need to put them again into the classes directory ;-)

您必须为 PDF 导出器提供正确的字体名称。您的字体显示在嵌入的字体 jar 中。您不需要再次将它们放入类目录中 ;-)

There is the correct fonts.xml content for pdf exporter (perhaps the names are case sensitive, be careful) :

pdf 导出器有正确的 fonts.xml 内容(也许名称区分大小写,请注意):

<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
<fontFamily name="Arial">
    <normal>
      <ttf>fonts/Arial/ARIAL.TTF</ttf>
      <pdf>fonts/Arial/ARIAL.TTF</pdf>
    </normal>
    <bold>
      <ttf>fonts/Arial/ARIALBD.TTF</ttf>
      <pdf>fonts/Arial/ARIALBD.TTF</pdf>
    </bold>
    <italic>
      <ttf>fonts/Arial/ARIALI.TTF</ttf>
      <pdf>fonts/Arial/ARIALI.TTF</pdf>
    </italic>
    <boldItalic>
      <ttf>fonts/Arial/ARIALBI.TTF</ttf>
      <pdf>fonts/Arial/ARIALBI.TTF</pdf>
    </boldItalic>
    <pdfEncoding>Cp1252</pdfEncoding>
    <pdfEmbedded>true</pdfEmbedded>
    <exportFonts>
    </exportFonts>
</fontFamily>
</fontFamilies>

I hope this help.

我希望这会有所帮助。

Regards, T.

问候,T。

回答by TiyebM

In my case, I was using:

就我而言,我使用的是:

<font fontName="Arial" pdfEncoding="Identity-H" isPdfEmbedded="true"/>

I had to remove pdffontName, the correct font tag is:

我不得不删除pdffontName,正确的字体标签是:

<font fontName="Arial" pdfEncoding="Cp1256"/>

NoteI am using a font jar that contains the following .xml file:

注意我使用的字体 jar 包含以下 .xml 文件:

    <?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>

   <fontFamily name="Arial">
       <normal><![CDATA[fonts/arial.ttf]]></normal>
       <pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
       <pdfEmbedded><![CDATA[true]]></pdfEmbedded>
   </fontFamily>


</fontFamilies>