Jasperreports NulPointerException producing PDF/A files

1.5k Views Asked by At

I'm using this code to produce a PDF, all works well.

JasperReport jasperReport = (JasperReport) JRLoader.loadObject(new File(reportname));
JRDataSource ds = ... a datasource
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, d.getHashMap(), ds);
JasperExportManager.exportReportToPdfFile(jasperPrint, output_path + "p4.pdf");

When I try to produce a pdf compliant pdfa1b I obtains this exception

Exception in thread "main" java.lang.NullPointerException
    at com.itextpdf.text.pdf.internal.PdfA1Checker.checkPdfObject(PdfA1Checker.java:323)
    at com.itextpdf.text.pdf.internal.PdfAChecker.checkPdfAConformance(PdfAChecker.java:208)
    at com.itextpdf.text.pdf.internal.PdfAConformanceImp.checkPdfIsoConformance(PdfAConformanceImp.java:71)
    at com.itextpdf.text.pdf.PdfWriter.checkPdfIsoConformance(PdfWriter.java:3426)
    at com.itextpdf.text.pdf.PdfWriter.checkPdfIsoConformance(PdfWriter.java:3422)
    at com.itextpdf.text.pdf.PdfDictionary.toPdf(PdfDictionary.java:137)
    at com.itextpdf.text.pdf.PdfArray.toPdf(PdfArray.java:175)
    at com.itextpdf.text.pdf.PdfDictionary.toPdf(PdfDictionary.java:149)
    at com.itextpdf.text.pdf.PdfIndirectObject.writeTo(PdfIndirectObject.java:158)
    at com.itextpdf.text.pdf.PdfWriter$PdfBody.write(PdfWriter.java:420)
    at com.itextpdf.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:398)
    at com.itextpdf.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:334)
    at com.itextpdf.text.pdf.PdfWriter.addToBody(PdfWriter.java:829)
    at com.itextpdf.text.pdf.PdfWriter.close(PdfWriter.java:1311)
    at com.itextpdf.text.pdf.PdfAWriter.close(PdfAWriter.java:336)
    at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:886)
    at com.itextpdf.text.Document.close(Document.java:416)
    at net.sf.jasperreports.engine.export.JRPdfExporter.exportReportToStream(JRPdfExporter.java:830)
    at net.sf.jasperreports.engine.export.JRPdfExporter.exportReport(JRPdfExporter.java:519)
    at net.sf.jasperreports.engine.JasperExportManager.exportToPdfFile(JasperExportManager.java:157)
    at net.sf.jasperreports.engine.JasperExportManager.exportReportToPdfFile(JasperExportManager.java:505)
    at ga.jr.run.RunMe.main(RunMe.java:45)

this is a portion of my jasper file

<property name="net.sf.jasperreports.export.pdf.tagged" value="true"/>
<property name="net.sf.jasperreports.export.pdfa.conformance" value="pdfa1b"/>
<property name="net.sf.jasperreports.export.pdfa.icc.profile.path" value="Z:\\Produzione\\Development\\TEMPLATE\\sRGB_v4_ICC_preference.icc"/>

No exception if I remove the net.sf.jasperreports.export.pdfa.conformance (or set it to "none"), but the file is not a PDF/A

what is wrong? thanks

EDIT: I'm using iText 5.5.3 (same issue using itext 5.5.0) and Jasperreports 6.0.0

EDIT: tested using AdobeRGB1998.icc and sRGB_v4_ICC_preference.icc, ISOnewspaper26v4_gr.icc and ISOnewspaper26v4.icc, eciRGB_v2.icc and ISOcoated_v2_300_eci.icc (found in my PDFCreator installation directory) same exception

1

There are 1 best solutions below

1
On

This is a bug. I reverted to version 5.6.1 and all works ok.

Today is 17/12/2014 and I'm waiting for a Jasperreports version above 6.0.0.

Bug documented at http://community.jaspersoft.com/jasperreports-library/issues/3988

When i try to create a PDF/A document with version 6.0.0 of JasperReports Library i always get a NullPointerException at com.itextpdf.text.pdf.internal.PdfA1Checker.checkPdfObject, line no. 332.

Exactly the same report works fine with 5.6.1 (using iText 2.1.7js2) exported as PDF/A.

If we take a look at the source of PdfA1Checker, we see (starting at line 327):

PdfObject iccProfileStream = dictionary.get(PdfName.DESTOUTPUTPROFILE); String inputColorSpace = ""; if (iccProfileStream != null) { ICC_Profile icc_profile = writer.getColorProfile(); // Always returns null try { inputColorSpace = new String(icc_profile.getData(), 16, 4, "US-ASCII"); // NPE!!! on access to icc_profile } catch (UnsupportedEncodingException e) { throw new ExceptionConverter(e); } }

I did a debug session and found out that writer.getColorProfile() always returns null. This seems to be the case because JRPdfExporter does NOT initialize it. So, i guess, this issue is a side effect of the upgrade to iText 5.5.0. (I think PdfWriter.setOutputIntents must be called to set the icc_profile correctly...)

Hope this helps, fixing this.

Regards, Michael

v6.0.0 JRPdfExporter