Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>A simple command line executable called: <em>pdfinfo</em>.</h2> <p>It is <a href="http://www.foolabs.com/xpdf/download.html">downloadable for Linux and Windows</a>. You download a compressed file containing several little PDF-related programs. Extract it somewhere.</p> <p>One of those files is <strong>pdfinfo</strong> (or <strong>pdfinfo.exe</strong> for Windows). An example of data returned by running it on a PDF document:</p> <pre class="lang-none prettyprint-override"><code>Title: test1.pdf Author: John Smith Creator: PScript5.dll Version 5.2.2 Producer: Acrobat Distiller 9.2.0 (Windows) CreationDate: 01/09/13 19:46:57 ModDate: 01/09/13 19:46:57 Tagged: yes Form: none Pages: 13 &lt;-- This is what we need Encrypted: no Page size: 2384 x 3370 pts (A0) File size: 17569259 bytes Optimized: yes PDF version: 1.6 </code></pre> <p>I haven't seen a PDF document where it returned a false pagecount (yet). It is also really fast, even with big documents of 200+ MB the response time is a just a few seconds or less.</p> <p>There is an easy way of extracting the pagecount from the output, here in PHP:</p> <pre class="lang-php prettyprint-override"><code>// Make a function for convenience function getPDFPages($document) { $cmd = "/path/to/pdfinfo"; // Linux $cmd = "C:\\path\\to\\pdfinfo.exe"; // Windows // Parse entire output // Surround with double quotes if file name has spaces exec("$cmd \"$document\"", $output); // Iterate through lines $pagecount = 0; foreach($output as $op) { // Extract the number if(preg_match("/Pages:\s*(\d+)/i", $op, $matches) === 1) { $pagecount = intval($matches[1]); break; } } return $pagecount; } // Use the function echo getPDFPages("test 1.pdf"); // Output: 13 </code></pre> <p>Of course this command line tool can be used in other languages that can parse output from an external program, but I use it in PHP.</p> <p><strong>I know its not pure PHP</strong>, but external programs are <em>way</em> better in PDF handling (as seen in the question).</p> <p>I hope this can help people, because I have spent a whole lot of time trying to find the solution to this and I have seen a lot of questions about PDF pagecount in which I didn't find the answer I was looking for. That's why I made this question and answered it myself.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload