We have a standard PDF in the form of a ByteArrayOutputStream we need to convert to PDF/A and return as a byte array (not saving the file).
When using the overload: converter.toPdfA1A("output file path") , the saved file passes PDF/A validation. However, when using the overload converter.toPdfA1A(OutputStream), the resulting pdf loaded on the browser does not pass PDF/A validation. The validation error I get is:
"The object number and generation number are not separated by a single white-space character..."
Here is my code so far (Thanks in advance):
- Code: Select all
public static byte[] toPdfa(String htmlString) throws IOException, DocumentException {
Document doc = null;
ByteArrayOutputStream baos = null;
try{
baos = new ByteArrayOutputStream();
//using iText PDF 7
PdfWriter writer = new PdfWriter(baos);
doc = HtmlConverter.convertToDocument(htmlString, writer);
doc.close();
InputStream bais = new ByteArrayInputStream(baos.toByteArray());
PdfStandardsConverter converter = new PdfStandardsConverter(bais);
//Convert to PdfA1A:
converter.toPdfA1A("C:/Users/mgutierrez/Desktop/PDFA/" + "ToPdfA1A.pdf"); // the output file passes PDF/A validation
//Now try the same but return a byte array instead.
converter.toPdfA1A(baos);
return baos.toByteArray(); // byte array is returned to the client and loaded in a PDF object in the browser. (the resulting document looks like the saved file but it does not pass validation)
}
catch(DocumentException | IOException ex){
return null;
}
finally {
try {baos.close();} catch (Exception eClose) {}
}
}