user3207455
user3207455

Reputation: 73

'com.itextpdf.text.exceptions.InvalidPdfException: PDF header signature not found' when reading the input stream from servlet

I am creating a editable pdf from a servlet. The user enters data and clicks the submit button. The action for the submit button is defined as "button.setAction(PdfAction.createSubmitForm("/fdf", null, PdfAction.SUBMIT_XFDF))".

When I try to read the form fields back into the server using FdfReader reader = new FdfReader (request.getInputStream()), I keep getting the above error of 'PDF header signature not found'. On debugging where the error is, the pdf generated has the '%PDF-'. But somehow when the inputstream is being read, I get this error.

Any help would be greatly appreciated.

Upvotes: 1

Views: 1619

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

You say that you submit the data as XFDF (which is the XML version of FDF). However, you try reading this data using FdfReader instead of XfdfReader. FdfReader expects a file that is written in PDF syntax and that starts with %PDF-1. You are receiving a file that is written in XML and that starts with:

<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">

As <?xml is quite different from %PDF-1 (which is the PDF header signature), you shouldn't be surprised by the error. It says exactly what is wrong with the data you're feeding to FdfReader.

Replace FdfReader with XfdfReader in your code and your problem is solved.

Upvotes: 1

Related Questions