I have a pdf file that is causing NPE in Aspose pdf converter. Steps to reproduce:
- Have this file ns.pdf (49.4 KB)
- Create new
com.aspose.pdf.Document
from this file. - Convert this document to PDFA (for example PDF_A_3B) with options ConvertErrorAction.Delete.
- Save conversion result to byte array.
- Create new
com.aspose.pdf.Document
from this byte array. - 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;
}