Appearance
How to Count the Pages in a PDF File with PHP
PHP provides a range of extensions and functions for getting the number of pages in a PDF document.
Here, we will check out the different methods used for that purpose.
Applying the ImageMagick Extension
The ImageMagick extension of PHP is capable of understanding a PDF document. The underlying command is identify. In PHP, you can count the number of pages using the Imagick::getNumberImages() method.
Note: Ensure the imagick extension is installed and enabled in your PHP environment (e.g., sudo apt install php-imagick on Debian/Ubuntu).
php
<?php
try {
$imagick = new Imagick('document.pdf');
$pageCount = $imagick->getNumberImages();
echo $pageCount;
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
?>Using the TCPDI Class
An alternative method is using the TCPDI class, which is part of the TCPDF library.
So, the code to use is shown below:
php use tcpdi function
php
<?php
require_once 'tcpdf/tcpdi.php'; // Requires TCPDF library
$fileName = 'document.pdf';
$tcpdi = new TCPDI();
$tcpdi->setSourceData((string) file_get_contents($fileName));
$pageCount = $tcpdi->getNumberOfPages();
echo $pageCount;
?>Note: TCPDI is deprecated in modern TCPDF versions. For current projects, use the standard TCPDF class or a maintained fork. Calling setSourceData() loads the PDF metadata into memory, allowing getNumberOfPages() to read the page count without needing to import or render individual pages.
Using pdfinfo
A faster method for Linux users is pdfinfo. It counts the number of pages in a PDF file quite quickly.
Note that you must install pdfinfo (part of the poppler-utils package) before running the command. On Debian/Ubuntu, run sudo apt install poppler-utils. On Windows, it is available as pdfinfo.exe.
The command to use looks as follows:
php use pdfinfo
php
<?php
$pdfPath = 'document.pdf';
// Use grep to robustly match the Pages line, then extract the number
exec('/usr/bin/pdfinfo ' . escapeshellarg($pdfPath) . ' | grep -i "Pages:" | awk \'{print $2}\'', $output);
$pageCount = (int) ($output[0] ?? 0);
?>Security Note: Always use escapeshellarg() when passing file paths to exec() to prevent command injection risks. Avoid passing untrusted user input directly to shell commands.
Parsing PDF as a Text File
In this section, you will see the PHP code that gets the number of pages in a PDF document.
Within the code, a function countPdfPages is created that contains the logic.
Within the $path variable, the path of the PDF whose page count should be determined is written.
It is essential to write the correct path.
Here is an example:
php count page numbers of pdf
php
<?php
$path = 'pdf/GFG.pdf';
if (!file_exists($path)) {
echo "File not found";
exit;
}
$totalPages = countPdfPages($path);
echo $totalPages;
function countPdfPages($path)
{
$pdf = file_get_contents($path);
// Note: This regex works reliably only on uncompressed PDFs.
// It may fail on compressed, encrypted, or binary-heavy PDFs.
$number = preg_match_all("/\/Type\s*\/Page\b/", $pdf, $dummy);
return $number;
}
?>output of php count page numbers of pdf
console
4