Jasper报告Unicode字体未显示在PDF中

时间:2018-11-04 11:23:19

标签: jasper-reports export-to-pdf

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

虽然报告已在jasper studio中正确呈现,但是当我从应用程序中使用报告时,却没有unicode字体显示用于PDF导出。对于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中显示)

enter image description here

0 个答案:

没有答案