0

I have a Jasper Report template that has English text as well as text in a language called Pashto (Pashto is a language that has more characters than Arabic and has all the characters of Arabic Langage).

My JRXML has the following content.

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.13.0.final using JasperReports Library version 6.13.0-46ada4d1be8f3c5985fd0b6146f3ed44caed6f05  -->
<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="birth registration" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" resourceBundle="localizationdemo" uuid="c07e9faf-a34d-48bd-bbab-962a662c7110">
    <property name="ireport.zoom" value="1.5"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="205"/>
    <style name="table">
        <box>
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="Table Dataset 1" uuid="f0720649-b198-4500-815e-f6f9e5fbafef"/>
    <queryString language="SQL">
        <![CDATA[select * from birth where id in (select id from birth limit 1)]]>
    </queryString>
    <title>
        <band height="773" splitType="Stretch">
            <rectangle>
                <reportElement x="21" y="41" width="516" height="23" uuid="3a6b356d-f32d-41fd-a28d-3895189070d4"/>
            </rectangle>
            <line>
                <reportElement x="0" y="4" width="555" height="1" uuid="906b4539-daeb-4afe-94cf-f52db3059c25"/>
            </line>
            <line>
                <reportElement x="555" y="4" width="1" height="757" uuid="272adf95-1af4-49b8-8cc0-8c50a522bfe1"/>
            </line>
            <line>
                <reportElement x="0" y="4" width="1" height="757" uuid="aab9580d-1e08-4081-b974-e8fefc49b000"/>
            </line>
            <line>
                <reportElement x="0" y="760" width="555" height="1" uuid="0f2d922f-93af-4f28-b8a3-f07366aa2ddd"/>
            </line>
            <staticText>
                <reportElement x="22" y="40" width="100" height="30" uuid="4d9b3efd-1d2a-48a7-947f-afabbb4f5afb"/>
                <text><![CDATA[Name]]></text>
            </staticText>
            <line>
                <reportElement x="0" y="760" width="555" height="1" uuid="0f2d922f-93af-4f28-b8a3-f07366aa2ddd"/>
            </line>
            <staticText>
                <reportElement x="124" y="40" width="100" height="30" uuid="4d9b3efd-1d2a-48a7-947f-afabbb4f5afb"/>
                <text><![CDATA[Father's Name]]></text>
            </staticText>
            <line>
                <reportElement x="0" y="760" width="555" height="1" uuid="0f2d922f-93af-4f28-b8a3-f07366aa2ddd"/>
            </line>
            <staticText>
                <reportElement x="225" y="40" width="100" height="30" uuid="4d9b3efd-1d2a-48a7-947f-afabbb4f5afb"/>
                <text><![CDATA[پښتو لیک]]></text>
            </staticText>
        </band>
    </title>
</jasperReport>

My Spring boot Controller is

    @GetMapping(value = "/download/{reportFileName}/{locale}")
    public void downloadReport(final HttpServletResponse response, final HttpServletRequest request
            , @PathVariable("reportFileName") String reportFileName, @PathVariable("locale") String locale) throws IOException, JRException,SQLException ,Exception
    {
        try{
            final HashMap<String, Object> parameters = new HashMap<>();
    
            parameters.put(JRParameter.REPORT_LOCALE, new Locale(locale));

            String returnedPath = this.service.generatePdfJasperReportFromJRXMLFile(parameters, "/certificates/birth_registration/" + reportFileName, "pdf", locale);

            this.service.downloadReport(returnedPath, response);
        }
        catch(Exception e){
            System.out.println("Some error has occurred while preparing the pdf AnarReport.---------------" + e.getMessage());
            e.printStackTrace();
        }
    }

My Service file has these functions

    public String generatePdfJasperReportFromJRXMLFile(HashMap<String, Object> parameters, String jrxmlFile, String reportType, String locale) throws IOException, JRException,SQLException {
        /**
            This function generates a pdf report from a .jrxml file stored in the classpath
            
            A Usage Example can be:

                final Map<String, Object> parameters = new HashMap<>();
                parameters.put("name", "province name");
                String returnedPath = service.generatePdfJasperReportFromJRXMLFile(new HashMap<>(parameters), "/test-report.jrxml")

            @return - returns the path of the destination file.
         */

        // Fetching the .jrxml file from the resources folder.
        final InputStream stream = this.getClass().getResourceAsStream(jrxmlFile); 
        String pdfFilePath = this.printJasperReport(stream, parameters, reportType);

        return pdfFilePath;
    }

    private String printJasperReport(InputStream stream, Map<String, Object> parameters, String reportType) throws IOException, JRException,SQLException{
        
        /**
            This function generates a pdf file from the given stream with the given parameters and saves it into a 
            a temporary file.

            @return - returns the path of the temp file.
         */

        System.out.println("parameters+++++++++++++++++++++++++++++" + parameters);
        // Compile the Jasper report from .jrxml to .japser
        final JasperReport report = JasperCompileManager.compileReport(stream);
 
        // The URL would look something like "jdbc:postgresql://localhost:5432/ebreshna_test?user=some_user&password=some_password"
        String url = String.format("%s?user=%s&password=%s", this.datasourceUrl, this.datasourceUsername, this.datasourcePassword);
        
        // The Generated PDF file will be stored in a temp directory
        String destFile = "";

        try{
            Connection conn = DriverManager.getConnection(url);
            
            final JasperPrint print = JasperFillManager.fillReport(report, parameters, conn);
            // Export the report to a PDF file.

            if(reportType.equals("pdf")){
                destFile = File.createTempFile("temp-jasper-report", ".pdf").getPath();
                JasperExportManager.exportReportToPdfFile(print, destFile);
            }
            else if(reportType.equals("excel")){

                destFile = File.createTempFile("temp-jasper-report", ".xlsx").getPath();

                // JRXlsExporter exporter = new JRXlsExporter();
                JRExporter exporter = new JRXlsExporter();
                exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
                exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
                exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
                //we set the one page per sheet parameter here
                exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
                
                exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
                exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile);
  
                // exporter.setParameter(JRExporterParameter.INPUT_STREAM, print);
                // exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile);
                exporter.exportReport();
            }
            else if(reportType.equals("csv")){
                destFile = File.createTempFile("temp-jasper-report", ".csv").getPath();
                
                JRCsvExporter exporter = new JRCsvExporter();
                exporter = new JRCsvExporter();
                exporter.setExporterInput(new SimpleExporterInput(print));
                exporter.setExporterOutput(new SimpleWriterExporterOutput(new File(destFile)));
                SimpleCsvExporterConfiguration configuration = new SimpleCsvExporterConfiguration();
                configuration.setWriteBOM(Boolean.TRUE);
                configuration.setRecordDelimiter("\r\n");
                exporter.setConfiguration(configuration);
                exporter.exportReport();
            }
        }catch(Exception e){
            System.out.println("Some error has occurred while preparing the pdf AnarReport." + e.getMessage());
            e.printStackTrace();
        }

        return destFile;
    }

    public void downloadReport(String filePath, HttpServletResponse response) throws Exception{
        File file = new File(filePath);
   
        if (file.exists()) {
            fileDownloadUtil.fileDownload(file, response);
        }
    } 

Now the problem is that the text of Pashto language is not displayed after downloading the pdf file.

I would appreciate help with this.

Edits

After a lot of try and fails, I used Arial font which works but i still have problems with the output.

This is the current result of spring boot generating a report.

enter image description here

Correct result would be the one that is currently displayed in jasper studio.

enter image description here

Walid Mashal
  • 342
  • 1
  • 12

1 Answers1

0

firstly create and add the font extension (jar) file into project classpath as per the tutorial. then set the font name and other properties as follows,

as per the following example, firstly create Saraiki.jar (font extension) file for Saraiki font (Saraiki font support the Pashto language). then add it to project classpath. if you want to use this font, you should use the defined font name of font extension file. however when set any other font name (undefined font name), this font not support.

<staticText>
    <reportElement x="225" y="40" width="100" height="30" uuid="4d9b3efd-1d2a-48a7-947f-afabbb4f5afb"/>
    <textElement>
        <font fontName="Saraiki" size="10" isBold="false"/>
    </textElement>
    <text><![CDATA[پښتو لیک]]></text>
</staticText>
Lakshan
  • 1,404
  • 3
  • 10
  • 23