PDF header signature not found

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

PDF header signature not found

ktanzer
This post has NOT been accepted by the mailing list yet.

I'm trying to open a PDF using the itextpdf-5.5.9.jar library, but am getting the "PDF header signature not found"  exception as illustrated below.  The file being read appears to be a valid PDF (%PDF-1.4) after writing the inputStream to a file, and is also attached.test1.zip
The file renders correctly using MacOS preview, Acrobat Reader and other tools.

com.itextpdf.text.exceptions.InvalidPdfException: PDF header signature not found.
        at com.itextpdf.text.pdf.PRTokeniser.getHeaderOffset(PRTokeniser.java:229)
        at com.itextpdf.text.pdf.PdfReader.getOffsetTokeniser(PdfReader.java:497)
        at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:176)
        at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:395)
        at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:415)
        at com.hco.processor.InboundFileProcessorThread.getPDFPageCount(.........
 
the method throwing the exception is:
private int getPDFPageCount(InputStream inputStream) {
        if (LOG.isTraceEnabled()) LOG.trace(LogHelper.getMethodName(LogHelper.THIS_METHOD) + " enter");
        int result = 0;

        try {
                PdfReader reader = new PdfReader(inputStream);
                result = reader.getNumberOfPages();
                reader.close();
               
        } catch (IOException e) {
                LOG.error(LogHelper.getMethodName(LogHelper.THIS_METHOD) + " caught IOException",e);
        } //end-catch
       
        if (LOG.isTraceEnabled()) LOG.trace(LogHelper.getMethodName(LogHelper.THIS_METHOD) + " result = " + result);
        return result;
}

Reply | Threaded
Open this post in threaded view
|

Re: PDF header signature not found

ktanzer
Addendum:
If I use the physical file to get the page count, using the similar function below, it works!  Why does writing the file to disk, versus using an InputStream object, do something to flush/fix/correct the document?

private int getPDFPageCount(String nameFile) {
        if (LOG.isTraceEnabled()) LOG.trace(LogHelper.getMethodName(LogHelper.THIS_METHOD) + " enter");
        int result = 0;

        try {
                PdfReader reader = new PdfReader(nameFile);
                result = reader.getNumberOfPages();
                reader.close();
        } catch (IOException e) {
                LOG.error(LogHelper.getMethodName(LogHelper.THIS_METHOD) + " caught IOException",e);
        } //end-catch
       
        if (LOG.isTraceEnabled()) LOG.trace(LogHelper.getMethodName(LogHelper.THIS_METHOD) + " result = " + result);
        return result;
}
Reply | Threaded
Open this post in threaded view
|

Re: PDF header signature not found

blowagie
You probably create the InputStream incorrectly. Remove iText from your test and read the first characters from your InputStream to a file. You'll see that the file read from the InputStream doesn't start with %PDF-1.4