Creating Hindi PDF using UTF-8 in JasperReport

2019-08-27 17:56发布

My requirement is to Create a PDF with some text in Hindi/Marathi/Other Indian Language while some text in English.

Text in Hindi/Marathi/Other Indian Language will be static text in Unicode (UTF-8) while text in English will be static and dynamic. I created a jrxml file in iReport 5.5.0. In iReport Internal Viewer, it shows proper output. However while exporting in pdf, it doesn't show the Hindi Text at all. It only shows English Text. While exporting in doc, the Hindi Text is coming as squares which I feel is word setting issue. In HTML or RTF export, it works perfectly. The same thing is happening when I create pdf/doc/html/rtf using Java Code. I am attaching Java and jrxml file over here for reference.

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();
                }
            }
        }
    }
}

The JRXML File is :

    <?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条回答
登录 后发表回答