Null Pointer Exception (NPE) when saving PDF document that was converted to PDFA

I have a pdf file that is causing NPE in Aspose pdf converter. Steps to reproduce:

  1. Have this file ns.pdf (49.4 KB)
  2. Create new com.aspose.pdf.Document from this file.
  3. Convert this document to PDFA (for example PDF_A_3B) with options ConvertErrorAction.Delete.
  4. Save conversion result to byte array.
  5. Create new com.aspose.pdf.Document from this byte array.
  6. Save his document to file.

Step 6 produces NPE:

java.lang.NullPointerException
    at com.aspose.pdf.internal.l10t.ly.lI(Unknown Source)
    ...
    at Main.main(Main.java:51)

It seems that this pdf file has some javascript code in it that makes Aspose behave wrong. Somehow if I don’t set ConversionOptions to Delete then Aspose doesn’t even convert a file to PDFA and convert() returns false. But out production uses option Delete and we need a way to convert PDF similar to this to PDFA. What do we do?

My system data:

Ubuntu 20.04.3 LTS
openjdk 11.0.10 2021-01-19
Aspose version: 21.12

Here is the code to reproduce an error:

public static void main(String[] args) {

    com.aspose.pdf.License pdfLicense = new com.aspose.pdf.License();
    try {
        pdfLicense.setLicense("../conf/Aspose.Total.Java.lic");
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    File inputFile = new File("../samples/ns.pdf");
    try {

        // reading source file to document
        com.aspose.pdf.Document doc = new Document(new FileInputStream(inputFile));

        // converting source document to PDFA
        Document convertedDoc = Main.toPDFA(doc);

        // saving PDF_A_3B document to byte array
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        convertedDoc.save(baos);
        byte[] content = baos.toByteArray();

        // we have some conversions here in production

        // getting document again from byte array
        Document reopenedDoc = new Document(new ByteArrayInputStream(content));

        // saving new document to file
        reopenedDoc.save("../output/ns_converted.pdf");
    }
    catch (Exception e) {

        e.printStackTrace();
    }
}

public static com.aspose.pdf.Document toPDFA(com.aspose.pdf.Document pdfDocument) {

    Path tempFilePath = null;
    try {
        tempFilePath = Files.createTempFile(null, "tmpfilename");
    } catch (IOException e) {
        e.printStackTrace();
    }
    String fileName = tempFilePath.toAbsolutePath().toString();

    PdfFormatConversionOptions conversionOptions = new PdfFormatConversionOptions(
        fileName,
        PdfFormat.PDF_A_3B,
        ConvertErrorAction.Delete
    );
    boolean converted = pdfDocument.convert(conversionOptions);
    System.out.println("converted: " + converted);

    return pdfDocument;
}

@whitebearspirit

We were able to reproduce the issue in our environment while testing the scenario with the latest version of the API. Therefore, it has been logged as PDFJAVA-41555 in our issue tracking system. We will further look into its details and keep you posted with the status of its rectification. Please be patient and spare us some time.

We are sorry for the inconvenience.

1 Like