number of pages in a pdf file

前端 未结 3 1598
南旧
南旧 2020-12-15 10:10

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

相关标签:
3条回答
  • 2020-12-15 10:39
    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

    0 讨论(0)
  • 2020-12-15 10:42

    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.

    0 讨论(0)
  • 2020-12-15 10:59

    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.

    0 讨论(0)
提交回复
热议问题