Julie
Julie

Reputation: 209

number of pages in a pdf file

Does anyone know how I can count the number of pages in a pdf file using php? Thanks!

Upvotes: 11

Views: 15719

Answers (4)

Ridho Human Daryata
Ridho Human Daryata

Reputation: 1

// Put an image on the page
if(!isset($this->images[$file]))
{
    // First use of this image, get info
    if($type=='')
    {
        $pos = strrpos($file,'.');
        if(!$pos)
            $this->Error('Image file has no extension and no type was specified: '.$file);
        $type = substr($file,$pos+1);
    }
    $type = strtolower($type);
    if($type=='jpeg')
        $type = 'jpg';
    $mtd = '_parse'.$type;
    if(!method_exists($this,$mtd))
        $this->Error('Unsupported image type: '.$type);
    $info = $this->$mtd($file);
    $info['i'] = count($this->images)+1;
    $this->images[$file] = $info

Upvotes: -2

kenorb
kenorb

Reputation: 166319

exec('pdftops ' . $filename . ' - | grep showpage | wc -l', $output);

See as well the similar question and answers:

Count the number of pages in a PDF in only PHP

Upvotes: 0

fuenfundachtzig
fuenfundachtzig

Reputation: 8352

Based on R Ubben's answer I found the following PHP code to give good results:

function count_pages($pdfname) {
  $pdftext = file_get_contents($pdfname);
  $num = preg_match_all("/\/Page\W/", $pdftext, $dummy);
  return $num;
}

\W matches any non-alphanumeric character and excludes things like /Pages, /PageMode etc.

Upvotes: 19

R Ubben
R Ubben

Reputation: 2204

PDFs store pages in a tree. "/Pages" objects can have a "/Parent" and "/Kids" entries, followed by a "/Count". You can't sum the "/Count" entries because a Kid might be another Pages node. The "/Page" object is the leaf.

Open the pdf as a text file and count the number of times "/Page" (not "/Pages") appears in the file. That should be correct most of the time.

Upvotes: 5

Related Questions