Search code examples
javajasper-reportsencodeexport-to-pdf

Special characters on Jasper Report from Java Primefaces


I'm trying to print characters like this:

°µ± Ω≥≤Δα

I'm not having a problem with the first 3, but the second set of characters are not being printed when I compile and execute the report from Primefaces App.

Here's the code I'm using to print the report:

try {
    fc = FacesContext.getCurrentInstance();
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ec.responseReset();
    ec.setRequestCharacterEncoding("UTF-8");
    ec.setResponseHeader("Content-Disposition", "inline; filename=\""
            + "Report.pdf\"");// opens in same page
    ec.setResponseContentType("application/pdf");
    OutputStream output = ec.getResponseOutputStream();
    Connection jdbcConnection = datasource.getConnection();
    JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
    JasperReport jasperReport = JasperCompileManager
            .compileReport(jasperDesign);
    JasperPrint jasperPrint = JasperFillManager.fillReport(
            jasperReport, parameterMap, jdbcConnection);
    //JasperExportManager.exportReportToPdfStream(jasperPrint, output);
    JRPdfExporter pdf = new JRPdfExporter();
    pdf.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    pdf.setParameter(JRExporterParameter.OUTPUT_STREAM, output);
    pdf.exportReport();
    output.flush();
    output.close();
    fc.getCurrentInstance().responseComplete();
} catch (Exception e) {
    e.printStackTrace();
}

The report runs and print every character I need when testing from Jasper Studio. I think the main issue here is the encode of the PDF stream, but I'm open to suggestions.


Solution

  • I will answer myself. I just use Dejavu Sans in the report and put a maven dependency to the basic jasper fonts and that was it.

    <!-- Fonts for special characters -->
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports-fonts</artifactId>
            <version>6.0.0</version>
        </dependency>
    

    That's a fragment of my pom.xml

    Thanks to the guys in the comments who made it posible.