java 从 Web 应用程序以 PDF 格式部署 jasperreport 时字体不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43123323/
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-11-03 07:06:15  来源:igfitidea点击:

Fonts not working when deploying jasperreport in PDF from web app

javafontsjasper-reportsexport-to-pdf

提问by Lille

I'm having some issues with the fonts when I'm deploying the reports in my web app. The fonts I want to have in my result pdfs are Arial, Calibri and Century Gothic.

在 Web 应用程序中部署报告时,我遇到了一些字体问题。我想在结果 pdf 中使用的字体是 Arial、Calibri 和 Century Gothic。

I followed all the steps pointed herebut couldn't get it working.

我遵循了此处指出的所有步骤但无法使其正常工作。

Here's what I did:

这是我所做的:

I modified the content of my jasperreports_extension.propertieslike following:

我修改了我的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.dejavu=net/sf/jasperreports/fonts/fonts.xml
net.sf.jasperreports.extension.simple.font.families.arial=net/sf/jasperreports/fonts/fonts.xml
net.sf.jasperreports.extension.simple.font.families.calibri=net/sf/jasperreports/fonts/fonts.xml
net.sf.jasperreports.extension.simple.font.families.centurygothic=net/sf/jasperreports/fonts/fonts.xml

Then at net\sf\jasperreports\fontsI put the three folders for the fonts containing the four styles (normal, bold, italicand bold-italic) for each of the fonts before mentioned.

然后在net\sf\jasperreports\fonts 中,我为包含前面提到的每种字体的四种样式(正常、粗体斜体粗斜体)的字体放置了三个文件夹。

Also, modified the fonts.xmlat net/sf/jasperreports/fontsand it looks like this now:

另外,修改了net/sf/jasperreports/fonts 中fonts.xml,现在看起来像这样:

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

<fontFamilies>

    <fontFamily name="DejaVu Sans">
        <normal>net/sf/jasperreports/fonts/dejavu/DejaVuSans.ttf</normal>
        <bold>net/sf/jasperreports/fonts/dejavu/DejaVuSans-Bold.ttf</bold>
        <italic>net/sf/jasperreports/fonts/dejavu/DejaVuSans-Oblique.ttf</italic>
        <boldItalic>net/sf/jasperreports/fonts/dejavu/DejaVuSans-BoldOblique.ttf</boldItalic>
        <pdfEncoding>Identity-H</pdfEncoding>
        <pdfEmbedded>true</pdfEmbedded>
        <exportFonts>
            <export key="net.sf.jasperreports.html">'DejaVu Sans', Arial, Helvetica, sans-serif</export>
            <export key="net.sf.jasperreports.xhtml">'DejaVu Sans', Arial, Helvetica, sans-serif</export>
        </exportFonts>
        <!--
        <locales>
            <locale>en_US</locale>
            <locale>de_DE</locale>
        </locales>
        -->
    </fontFamily>

    <fontFamily name="DejaVu Serif">
        <normal>net/sf/jasperreports/fonts/dejavu/DejaVuSerif.ttf</normal>
        <bold>net/sf/jasperreports/fonts/dejavu/DejaVuSerif-Bold.ttf</bold>
        <italic>net/sf/jasperreports/fonts/dejavu/DejaVuSerif-Italic.ttf</italic>
        <boldItalic>net/sf/jasperreports/fonts/dejavu/DejaVuSerif-BoldItalic.ttf</boldItalic>
        <pdfEncoding>Identity-H</pdfEncoding>
        <pdfEmbedded>true</pdfEmbedded>
        <exportFonts>
            <export key="net.sf.jasperreports.html">'DejaVu Serif', 'Times New Roman', Times, serif</export>
            <export key="net.sf.jasperreports.xhtml">'DejaVu Serif', 'Times New Roman', Times, serif</export>
        </exportFonts>
    </fontFamily>

    <fontFamily name="DejaVu Sans Mono">
        <normal>net/sf/jasperreports/fonts/dejavu/DejaVuSansMono.ttf</normal>
        <bold>net/sf/jasperreports/fonts/dejavu/DejaVuSansMono-Bold.ttf</bold>
        <italic>net/sf/jasperreports/fonts/dejavu/DejaVuSansMono-Oblique.ttf</italic>
        <boldItalic>net/sf/jasperreports/fonts/dejavu/DejaVuSansMono-BoldOblique.ttf</boldItalic>
        <pdfEncoding>Identity-H</pdfEncoding>
        <pdfEmbedded>true</pdfEmbedded>
        <exportFonts>
            <export key="net.sf.jasperreports.html">'DejaVu Sans Mono', 'Courier New', Courier, monospace</export>
            <export key="net.sf.jasperreports.xhtml">'DejaVu Sans Mono', 'Courier New', Courier, monospace</export>
        </exportFonts>
    </fontFamily>

    <!--
        Mapping Java logical fonts, for the sake of HTML export only.
    -->

    <fontFamily name="SansSerif">
        <exportFonts>
            <export key="net.sf.jasperreports.html">'DejaVu Sans', Arial, Helvetica, sans-serif</export>
            <export key="net.sf.jasperreports.xhtml">'DejaVu Sans', Arial, Helvetica, sans-serif</export>
        </exportFonts>
    </fontFamily>
    <fontFamily name="Serif">
        <exportFonts>
            <export key="net.sf.jasperreports.html">'DejaVu Serif', 'Times New Roman', Times, serif</export>
            <export key="net.sf.jasperreports.xhtml">'DejaVu Serif', 'Times New Roman', Times, serif</export>
        </exportFonts>
    </fontFamily>
    <fontFamily name="Monospaced">
        <exportFonts>
            <export key="net.sf.jasperreports.html">'DejaVu Sans Mono', 'Courier New', Courier, monospace</export>
            <export key="net.sf.jasperreports.xhtml">'DejaVu Sans Mono', 'Courier New', Courier, monospace</export>
        </exportFonts>
    </fontFamily>

    <fontFamily name="Arial">
       <normal>net/sf/jasperreports/fonts/arial/arial.ttf</normal>
       <bold>net/sf/jasperreports/fonts/arial/ArialBold.ttf</bold>
       <italic>net/sf/jasperreports/fonts/arial/ArialItalic.ttf</italic>
       <boldItalic>net/sf/jasperreports/fonts/arial/ArialBoldItalic.ttf</boldItalic>
       <pdfEncoding>Identity-H</pdfEncoding>
       <pdfEmbedded>true</pdfEmbedded>
       <exportFonts>
           <export key="net.sf.jasperreports.html">'Arial'</export>
           <export key="net.sf.jasperreports.xhtml">'Arial'</export>
       </exportFonts>
    </fontFamily>

    <fontFamily name="Calibri">
       <normal>net/sf/jasperreports/fonts/calibri/calibri.ttf</normal>
       <bold>net/sf/jasperreports/fonts/calibri/calibri-bold.ttf</bold>
       <italic>net/sf/jasperreports/fonts/calibri/calibri-italic.ttf</italic>
       <boldItalic>net/sf/jasperreports/fonts/calibri/calibri-bold-italic.ttf</boldItalic>
       <pdfEncoding>Identity-H</pdfEncoding>
       <pdfEmbedded>true</pdfEmbedded>
       <exportFonts>
           <export key="net.sf.jasperreports.html">'Calibri'</export>
           <export key="net.sf.jasperreports.xhtml">'Calibri'</export>
       </exportFonts>
    </fontFamily>

    <fontFamily name="Century Gothic">
       <normal>net/sf/jasperreports/fonts/centurygothic/century-gothic.ttf</normal>
       <bold>net/sf/jasperreports/fonts/centurygothic/century-gothic-bold.ttf</bold>
       <italic>net/sf/jasperreports/fonts/centurygothic/century-gothic-italic.ttf</italic>
       <boldItalic>net/sf/jasperreports/fonts/centurygothic/century-gothic-bold-italic.ttf</boldItalic>
       <pdfEncoding>Identity-H</pdfEncoding>
       <pdfEmbedded>true</pdfEmbedded>
       <exportFonts>
           <export key="net.sf.jasperreports.html">'Century Gothic'</export>
           <export key="net.sf.jasperreports.xhtml">'Century Gothic'</export>
       </exportFonts>
    </fontFamily>   


</fontFamilies>

Note that I just added the last three fonts.

请注意,我刚刚添加了最后三种字体。

After that I prepared the jar file and put it in my app classpath as mandatory. So far I just got the report exported with normal Sanserif font...

之后,我准备了 jar 文件并将其作为必需的放在我的应用程序类路径中。到目前为止,我只是用普通的 Sanserif 字体导出了报告......

I don't know what I'm doing wrong, any ideas?

我不知道我做错了什么,有什么想法吗?

Thanks!

谢谢!

Quick informative note: I'm using Jaspersoft Studio Community edition (6.3.0) and my app is deployed in a JBoss server.

快速信息说明:我使用的是 Jaspersoft Studio 社区版 (6.3.0),我的应用程序部署在 JBoss 服务器中。

采纳答案by Lille

I finally found out what was happening in my case and now it's working.

我终于发现了我的案例中发生了什么,现在它正在起作用。

When I introduced the fonts (.ttf extension) in Jaspersoft Studio Windows->Preferences->FontsI selected a wrong value for the PDF Encoding selection combo. I first selected CP1252 (Western European ANSI aka WinAnsi)which is WRONGif you want to export reports in PDF, then I chose Identity-H (Unicode with horizontal writing)instead and it worked fine.

当我在 Jaspersoft Studio Windows->Preferences->Fonts 中引入字体(.ttf 扩展名)时,我为 PDF 编码选择组合选择了错误的值。我首先选择了CP1252(西欧 ANSI 又名 WinAnsi),如果您想以 PDF 格式导出报告,这是错误的,然后我选择了Identity-H(水平书写的 Unicode),并且效果很好。

However, what I still don't understand is: if in my report I'm only using Calibri and Century Gothic fonts, why in earth when I go to see the fonts used in my PDF (File->Properties->Fonts) I get also Helvetica in the list of fonts used?

但是,我仍然不明白的是:如果在我的报告中我只使用 Calibri 和 Century Gothic 字体,为什么在地球上我去查看 PDF 中使用的字体(File->Properties->Fonts)我在使用的字体列表中还获得 Helvetica 吗?

回答by Madhawa Madhuranga

  1. Export font from jasper report as extension (.jar)(tool/option/fonts).
  2. Add exported font jar to your project.

    https://community.jaspersoft.com/wiki/how-fix-problem-custom-fonts-jasperreports-server

  1. 将 jasper 报告中的字体导出为扩展名 (.jar)(工具/选项/字体)。
  2. 将导出的字体 jar 添加到您的项目中。

    https://community.jaspersoft.com/wiki/how-fix-problem-custom-fonts-jasperreports-server

回答by Ignatius Ojiambo

By including the below maven dependency in your pom the bold styling will be shown in the generated PDF

通过在 pom 中包含以下 maven 依赖项,粗体样式将显示在生成的 PDF 中

<dependency>
  <groupid>net.sf.jasperreports</groupid>
  <artifactid>jasperreports-fonts</artifactid>
  <version>6.0.0</version>
</dependency>