首页 > 解决方案 > Jasper 报告 unicode 字体未在 PDF 中显示

问题描述

我在 Spring MVC 中使用 jasper 报告。我从 jaspersoft studio 6.6.0 生成了 font.jar 并将其添加到我的应用程序类路径中。我正在尝试使用 vrinda.ttf 和 sutonnyOMJ.ttf。

虽然报告在 jasper studio 中正确呈现,但当我从我的应用程序中使用它时,没有显示用于 PDF 导出的 unicode 字体。对于 excel 导出,所有值都正确显示,但 pdf 不起作用。

我的自定义 font.jar 中的字体系列

<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
    <fontFamily name="vrinda">
        <normal><![CDATA[fonts/vrinda/vrinda.ttf]]></normal>
        <pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
        <pdfEmbedded><![CDATA[true]]></pdfEmbedded>
        <exportFonts>
           <export key="net.sf.jasperreports.html">vrinda</export>
           <export key="net.sf.jasperreports.xhtml">vrinda</export>
        </exportFonts>
    </fontFamily>
    <fontFamily name="sutonny">
        <normal><![CDATA[fonts/sutonny/SutonnyOMJ.ttf]]></normal>
        <bold><![CDATA[fonts/sutonny/SutonnyMJ-Bold.ttf]]></bold>
        <italic><![CDATA[fonts/sutonny/SutonnyMJ-Italic.ttf]]></italic>
        <boldItalic><![CDATA[fonts/sutonny/SutonnyMJ-BoldItalic.ttf]]></boldItalic>
        <pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
        <pdfEmbedded><![CDATA[true]]></pdfEmbedded>
        <exportFonts>
           <export key="net.sf.jasperreports.html">SutonnyOMJ</export>
           <export key="net.sf.jasperreports.xhtml">SutonnyOMJ</export>
        </exportFonts>
    </fontFamily>
</fontFamilies>

JRXML:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0  -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="myName" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a43ee182-4df8-4ea4-b26f-b8b21078c9d1">
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="dbname"/>
    <queryString language="SQL">
        <![CDATA[Query]]>
    </queryString>
    <field name="ClientName" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.label" value="ClientName"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="Table"/>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch">
            <textField>
                <reportElement x="20" y="5" width="500" height="30" uuid="f4900f30-da32-4cec-9142-10b111debcf3"/>
                <textElement>
                    <font fontName="Vrinda" size="26"/>
                </textElement>
                <textFieldExpression><![CDATA["\u09AA\u09CD\u09B0\u09A4\u09BF\u09AC\u09C7\u09A6\u09A8"]]></textFieldExpression>
            </textField>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch">
            <staticText>
                <reportElement x="82" y="0" width="100" height="30" uuid="b35f8d0e-5c47-46c7-b71c-c63dd7706724">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="9609ebcc-b3ae-4997-92b2-9aa48843434a"/>
                </reportElement>
                <textElement>
                    <font size="26"/>
                </textElement>
                <text><![CDATA[ClientName]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch">
            <textField>
                <reportElement x="82" y="64" width="308" height="30" uuid="ba9cab6a-2688-4601-bedb-3b62913a2cfb">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="9609ebcc-b3ae-4997-92b2-9aa48843434a"/>
                </reportElement>
                <textElement>
                    <font fontName="Vrinda" size="26"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{ClientName}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

Java 代码:

JasperReport jasperReport = JasperCompileManager.compileReport(reportSourceFile);
JasperPrint print = JasperFillManager.fillReport(jasperReport, params, reportRequestDTO.getConnection());
JRPdfExporter exporter = new JRPdfExporter();

ExporterInput exporterInput = new SimpleExporterInput(print);
exporter.setExporterInput(exporterInput);

OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(path);
exporter.setExporterOutput(exporterOutput);

SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
exporter.setConfiguration(configuration);

exporter.exportReport();

pom.xml

        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.5.1</version>
        </dependency>

        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports-fonts</artifactId>
            <version>6.0.0</version>
        </dependency>

标头 unicode 的 Excel 图像(未在 PDF 中显示)

在此处输入图像描述

标签: jasper-reportsexport-to-pdf

解决方案


推荐阅读