Jasper报告PDF格式的字符编码

Jasper报告PDF格式的字符编码

本文介绍了Jasper报告PDF格式的字符编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在jasper报告中使用自定义字体。 Myanmar3 是缅甸的标准字体。
一切正常,但 ReportTitle 必须缅甸字体 Myanmar3
Google 也使用 Myanmar3 用于缅甸语区域。

I am trying to use custom font in jasper report. Myanmar3 is the standard font of Myanmar.Everything is ok, but ReportTitle must be Myanmar Font with Myanmar3.Google also use Myanmar3 for Myanmar locale.

将报告导出为 html 文件。它可以显示报告标题。但是,导出的 PDF 文件无法正确显示。

When export the report as html file. It can display report title, well.But, Exported PDF file cannot display incorrectly.

在浏览器中。

PDF格式

public void report() throws Exception {
    List<SalesReport> saleReports = salesReportService.findSalesReport(new SalesReportCriteria());
    InputStream inputStream = new FileInputStream("report-template/saleReportTemplate.jrxml");
    String outputFilePdf = "D:/temp/BasicReport.pdf";
    String outputFileHtml = "D:/temp/BasicReport.html";
    Map paramMap = new HashMap();
    paramMap.put("ReportTitle", "\u1005\u101B\u1004\u103A\u1038\u1021\u1004\u103A\u1038\u1019\u103B\u102C\u1038\u1011\u100A\u103A\u101E\u103D\u1004\u103A\u1038\u1001\u103C\u1004\u103A\u1038");
    paramMap.put("TableDataSource", new JRBeanCollectionDataSource(saleReports));
    JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
    JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, paramMap, new JRBeanCollectionDataSource(saleReports));
    JasperExportManager.exportReportToPdfFile(jasperPrint, outputFilePdf);
    JasperExportManager.exportReportToHtmlFile(jasperPrint, outputFileHtml);
}

saleReportTemplate.jrxml

saleReportTemplate.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<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="TableReport" pageWidth="842" pageHeight="595" orientation="Landscape" whenNoDataType="AllSectionsNoDetail" columnWidth="802"
              leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30" isFloatColumnFooter="true" whenResourceMissingType="Empty" uuid="a255c602-4ff1-4db8-ab72-65b5c3ff9bdd">

    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <style name="Myanmar3" isDefault="true" fontName="Myanmar3" fontSize="10" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false"/>
    <style name="Table">
        <box leftPadding="0">
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="TableHeader" mode="Opaque" backcolor="#808080"/>
    <style name="TableFooter" mode="Opaque" backcolor="#C0C0C0"/>
    <subDataset name="TableData" uuid="41cd3dac-2d22-41b9-9872-8fdb465d0f85">
        <field ... for table generation/>
    </subDataset>
    <parameter name="TableDataSource" class="net.sf.jasperreports.engine.JRDataSource"/>
    <parameter name="ReportTitle" class="java.lang.String" isForPrompting="false"/>
    <title>
        <band height="153">
            <textField isBlankWhenNull="true">
                <reportElement uuid="b44cb7c1-f7d5-467c-8982-b95f65dcb849" x="106" y="0" width="573" height="59"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="22" isBold="true" isPdfEmbedded="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{ReportTitle}]]></textFieldExpression>
            </textField>
            <componentElement>
                -->other tag for table....
            </componentElement>
        </band>
    </title>
</jasperReport>

我已添加 MYANMAR3.TTF 文件和在 jasperreports-fonts-5.1.0.jar 中为 myanmar3 字体配置。

I already add MYANMAR3.TTF file and configure in jasperreports-fonts-5.1.0.jar for myanmar3 font.

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.dejavu=net/sf/jasperreports/fonts/fonts.xml
net.sf.jasperreports.extension.simple.font.families.myanmar3=net/sf/jasperreports/fonts/fonts.xml

font.xml

...other default config.

<fontFamily name="Myanmar3">
    <normal>net/sf/jasperreports/fonts/myanmar3/MYANMAR3.TTF</normal>
    <bold>net/sf/jasperreports/fonts/myanmar3/MYANMAR3.TTF</bold>
    <italic>net/sf/jasperreports/fonts/myanmar3/MYANMAR3.TTF</italic>
    <boldItalic>net/sf/jasperreports/fonts/myanmar3/MYANMAR3.TTF</boldItalic>
    <pdfEncoding>Identity-H</pdfEncoding>
    <pdfEmbedded>true</pdfEmbedded>
</fontFamily>

我测试了很多配置,参考和示例。但是,对 PDF 文件没有任何影响。

I have tested so many configuration, reference and example. But, there is no effect on PDF file.

如果您有任何建议,请告诉我。提供。

推荐答案

这是(版本测试5.5.4)和 ttf 字体,itext 不支持连字,请参阅下面的评论

This is a problem of iText (version tested 5.5.4) and the ttf font, itext doesn't support ligatures, see comment below

由于jasper-reports使用itext作为其库来呈现pdf,因此您可以在jasper报告中执行 nothing 解决这个问题。

Since jasper-reports is using itext as its library to render the pdf there is nothing you can do in jasper report to solve this problem.

使用仅iText 的示例代码将呈现相同的输出作为jasper报告

Example code using only iText will render same output as jasper report

public class FontTest {

    /** The resulting PDF file. */
    public static final String RESULT = "pdf/fontTest.pdf";
    /** Test text. */
    public static final String TEST = "\u1005\u101B\u1004\u103A\u1038\u1021\u1004\u103A\u1038\u1019\u103B\u102C\u1038\u1011\u100A\u103A\u101E\u103D\u1004\u103A\u1038\u1001\u103C\u1004\u103A\u1038";

    public void createPdf(String filename) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        document.open();
        BaseFont bf = BaseFont.createFont(
            "lib/myanmar3.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font font = new Font(bf, 20);
        ColumnText column = new ColumnText(writer.getDirectContent());
        column.setSimpleColumn(36, 730, 569, 36);
        column.addElement(new Paragraph(TEST, font));
        column.go();
        document.close();
    }

    public static void main(String[] args) throws IOException, DocumentException {
        new FontTest().createPdf(RESULT);
    }
}

我已发布跟进在部分,以了解为什么itext无法正确呈现字体。

I have posted a follow up question in itext section to understand why itext can't render font correctly.

这是(iText的原始开发者)的评论

This is the comment of @Bruno Lowagie (Original developer of iText) is

这篇关于Jasper报告PDF格式的字符编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 23:50