Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon Aug 02, 2021 6:48 pm

I am new to Spire and my organization is evaluating the product with intent to purchase if it meets our requirements.

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) {}
        }
    }

mgutierrez
 
Posts: 3
Joined: Mon Aug 02, 2021 4:59 pm

Tue Aug 03, 2021 8:52 am

Hello,

Thank you for your inquiry.
I did an initial test with your code, and indeed found that the generated PDF does not conform to the PDF/A format specification. I have logged the issue into our Bug tracking system with the ticket number SPIREPDF-4526. Our development team will investigate and fix it. Once it is resolved, I will inform you. Sorry for the inconvenience caused.
In addition, can you provide your verification URL to help us further confirm that our new version fixes can perfectly solve your problem? Thanks in advance.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Mon Aug 16, 2021 2:53 pm

Thank you, Annika. Is there an update on this ? Can you please clarify what you meant by verification URL? Currently, we are not able to provide a public test URL. Thanks again for your assistance.

mgutierrez
 
Posts: 3
Joined: Mon Aug 02, 2021 4:59 pm

Tue Aug 17, 2021 9:31 am

Hello,

Thanks for your follow up.
After further investigation, we found the cause of the issue is that the data of stream object "baos" was not cleared while saving the result document to the stream. To eliminate your issue, please clear the "baos" stream in your code or create a new stream. See the sample code below.
Code: Select all
        //...
        PdfStandardsConverter converter = new PdfStandardsConverter(bais);
        converter.toPdfA1A("ToPdfA1A_itext.pdf");
        // Reinitialize the stream
        baos = new ByteArrayOutputStream();
        //convert to PDFA1A
        converter.toPdfA1A(baos);
        return baos.toByteArray();

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Wed Aug 18, 2021 3:07 am

Thank you Annika. That works. It is validating correctly now.

mgutierrez
 
Posts: 3
Joined: Mon Aug 02, 2021 4:59 pm

Wed Aug 18, 2021 5:51 am

Hello,

You're welcome.
If you have other questions about using Spire.PDF in the future, please feel free to contact us.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.PDF