MiamiGenome has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have 100's of pdf files and would like to get the total page count. I tried using:
http://search.cpan.org/~antro/PDF-111/PDF.pm

but get the following error.
PDF::Core::PDFGetPrimitive() called too early to check prototype at /u +sr/local/lib/perl5/site_perl/5.8.8/PDF/Core.pm line 288. PDF::Core::PDFGetPrimitive() called too early to check prototype at /u +sr/local/lib/perl5/site_perl/5.8.8/PDF/Core.pm line 294. Can't read cross-reference section, according to trailer
Here was my code:
use PDF; $pdf=PDF->new("file.pdf"); print "Has ",$pdf->Pages," Pages \n";
Could someone please offer assistance? Many thanks!

Replies are listed 'Best First'.
Re: pdf - page count
by runrig (Abbot) on Jun 28, 2006 at 22:05 UTC
    That module hasn't been updated for a while. See if PDF-API2 or CAM-PDF work for you. They both have methods for getting number of pages.

    Update: CAM::PDF gives a similar error about missing cross-reference data for your sample gaba.pdf file.

Re: pdf - page count
by traveler (Parson) on Jun 28, 2006 at 22:00 UTC
    I would try:
    use PDF::API2; $pdf=PDF::API2->new("file.pdf"); print "Has ",$pdf->pages," Pages \n";
    Seems to work.
      Thanks for the suggestion. I get zero pages when I use the following file :
      http://www.wormbook.org/chapters/www_gaba/gaba.pdf

      This file should have 13 pages.
        OOPS, see my note above which should have been here.
Re: pdf - page count
by davidrw (Prior) on Jun 28, 2006 at 21:56 UTC
    looking at the source, i think that the first two lines are just warnings and unrelated to the third. The third is from this 'die' line in PDF::Parse in the ReadCrossReference_pass1() method:
    $_=PDF::Core::PDFGetline ($fd,\$offset); die "Can't read cross-reference section, according to trailer\n" if +! /xref\r?\n?/ ;
    what it means? i dunno.. Are you sure you have valid PDF files? can any other PDF module/program parse them correctly?
      The pdf is a standard file as far as I can tell. Adobe Reader and Acrobat opens it perfectly. The pdf is available at:
      http://www.wormbook.org/chapters/www_gaba/gaba.pdf
        It worked on my files. Yours gives an error:
        Malformed xref in PDF file at C:/Perl/site/lib/PDF/API2/Basic/PDF/Fil +e.pm line 1230.
        Looks like a bad file to me.