Search code examples
javaspringmavenjasper-reports

JasperReports unable to export PDF files


I'm trying to write simple code to export JasperPrint to pdf file. To be precise, write bytes into HTTP response.

My simple code looks like this:

public static void writePdfReport(final JasperPrint jp,
                                  final HttpServletResponse response,
                                  final String reportName) throws IOException, JRException {
    response.setContentType("application/pdf");
    response.setHeader("Content-disposition",
            "inline; filename=" + (reportName == null ? jp.getName() : reportName).replace('"', '_') + ".pdf");

    final OutputStream outStream = response.getOutputStream();

    final byte[] pdfBytes = JasperExportManager.exportReportToPdf(jp);
    response.setContentLength(pdfBytes.length);

    final ByteArrayInputStream bais = new ByteArrayInputStream(pdfBytes);
    IOUtils.copy(bais, outStream);

    outStream.flush();
    IOUtils.closeQuietly(outStream);
}

My Maven pom file:

 <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.8.1</version>
        </dependency>
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports-fonts</artifactId>
            <version>6.8.1</version>
        </dependency>
        <dependency>
            <groupId>ar.com.fdvs</groupId>
            <artifactId>DynamicJasper</artifactId>
            <version>5.1.2</version>
        </dependency>

The problem is that whenever I call writePdfReport function, I get this exception:

java.lang.NoClassDefFoundError: com/lowagie/text/pdf/FontMapper

    at net.sf.jasperreports.engine.JasperExportManager.exportToPdf(JasperExportManager.java:214)
    at net.sf.jasperreports.engine.JasperExportManager.exportReportToPdf(JasperExportManager.java:544)
    at com.kendaxa.fexcom.core.reports.ReportWriter.writePdfReport(ReportWriter.java:31)
    at com.kendaxa.fexcom.core.reports.GenericTableReportSpec.Should generate simple table PDF report(GenericTableReportSpec.groovy:100)
Caused by: java.lang.ClassNotFoundException: com.lowagie.text.pdf.FontMapper
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 4 more

I did my research, first, it seems that the problem is in the JasperReports library suggested here (4 years old): Dependency error in jasper-reports from itext

However, applying an accepted answer does not help, even with the newest version I get the same error.

I've also tried a different approach for exporting files inspired from here Export JasperReport to PDF OutputStream? without any luck.

Does anyone have an up-to-date working solution for exporting pdf files? Thank you for any hep.


Solution

  • The com.lowagie.text.pdf.FontMapper class does exist in the 2.1.7 version of com.lowagie.itext jar.

    Are you using this exact version as a dependency? In my working example this is the version I use.

    <dependency>
       <groupId>com.lowagie</groupId>
       <artifactId>itext</artifactId>
       <version>2.1.7</version>
    </dependency>
    

    If you are already doing that, I would make sure your runtime environment has only this 2.1.7 version of the itext jar. Is it possible another version of itext is getting included in the deployed jar library?

    I pushed a working example to https://github.com/cameronhurd/simple-jasper-report