在JasperReport中使用UTF-8创建印地语PDF

时间:2014-02-03 16:41:49

标签: java pdf utf-8 jasper-reports hindi

我的要求是使用印地语/马拉地语/其他印度语的某些文本创建PDF,而使用英语创建一些文本。

印地语/马拉地文/其他印度语文本将是Unicode(UTF-8)中的静态文本,而英文文本将是静态和动态的。我在 iReport 5.5.0 中创建了一个jrxml文件。在 iReport 内部查看器中,它显示正确的输出。但是,在以pdf格式导出时,它根本不显示印地语文本。它只显示英文文本。在文档中导出时,印地文文本就像广场一样,我认为是文字设置问题。在HTML或RTF导出中,它完美地运行。当我使用Java Code创建pdf / doc / html / rtf时,也会发生同样的事情。我在这里附上 Java jrxml 文件以供参考。

public class TestHindiPDF {
    public static void main(String[] args) throws Exception {

        String strJasperFilePath = null;
        String strJRXMLFilePath = null;
        String strReportPath = "D:\\Chintan\\Tech\\Input_Files\\TestMulti";
        String strSaveReportPath = "D:\\Chintan\\Tech\\Input_Files\\TestMulti";
        String strReportName = "multi1";

        strJasperFilePath = strReportPath + "/multi1.jasper";
        strJRXMLFilePath = strReportPath + "/multi1.jrxml";
        strReportName = "multi1";

        File reportFile = new File(strJasperFilePath);//your report_name.jasper file
        if (!reportFile.exists()) {
            try {
                System.out.println("Compiling JRXML File : " + strJRXMLFilePath);
                JasperCompileManager.compileReportToFile(strJRXMLFilePath, strJasperFilePath);
                reportFile = new File(strJasperFilePath);
            } catch (JRException e) {
                e.printStackTrace();
            }
        }

        if (reportFile != null) {
            OutputStream fileOut = null;
            try {
                JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile);

                JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, new JREmptyDataSource());

                JRExporter exporter2 = new JRRtfExporter();
                exporter2.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
                exporter2.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
                exporter2.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, strSaveReportPath + "/" + strReportName + ".rtf");
                exporter2.exportReport();

                JRExporter exporter3 = new JRHtmlExporter();
                exporter3.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
                exporter3.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
                exporter3.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, strSaveReportPath + "/" + strReportName + ".html");
                exporter3.exportReport();

                JRExporter exporter4 = new JRDocxExporter();
                exporter4.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
                exporter4.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
                exporter4.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, strSaveReportPath + "/" + strReportName + ".docx");
                exporter4.exportReport();

                JRExporter exporter1 = new JRPdfExporter();
                exporter1.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
                exporter1.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
                exporter1.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, strSaveReportPath + "/" + strReportName + ".pdf");
                exporter1.exportReport();


                JasperExportManager.exportReportToPdfStream(jasperPrint, fileOut);
            } catch (JRException e) {
                e.printStackTrace();
            } finally {
                if (fileOut != null) {
                    fileOut.close();
                }
            }
        }
    }
}

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="multi1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c8a053f6-858e-427b-aec1-89231aadddd6">
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement x="104" y="0" width="313" height="25" uuid="fa5e3e6e-26f7-4973-a2d5-3c3b7a5e8227"/>
                <textElement textAlignment="Center">
                    <font size="16"/>
                </textElement>
                <text><![CDATA[अतिदेय राशि के पुनर्भुगतान हेतु अनुस्मारक]]></text>
            </staticText>
            <staticText>
                <reportElement x="132" y="25" width="252" height="25" uuid="fa5e3e6e-26f7-4973-a2d5-3c3b7a5e8227"/>
                <textElement textAlignment="Center">
                    <font size="16"/>
                </textElement>
                <text><![CDATA[Reminder for repaying Overdue Amount]]></text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch"/>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

0 个答案:

没有答案