Count the total pages of a pdf file with PHP

Asked

Viewed 417 times

0

I’m trying to count the total pages of a pdf file, using php. I did a google search and found the code below, which works for files up to 10mb.

I also used the Pdfparser library, but I also have the same problem with the file size, the difference is that with Pdfparser it is possible to use files with up to 41mb.

I tried to increase the upload limit in php.ini, but I was unsuccessful. If anyone can give me a light already thank you very much. rs

ps: I’m a beginner in php.

<?php
/**
 * Esta función devuelve el número de páginas de un archivo pdf
 * Tiene que recibir la ubicación y nombre del archivo
 */
function numeroPaginasPdf($archivoPDF)
{
    $stream = fopen($archivoPDF, "r");
    $content = fread ($stream, filesize($archivoPDF));

    if(!$stream || !$content)
        return 0;

    $count = 0;

    $regex  = "/\/Count\s+(\d+)/";
    $regex2 = "/\/Page\W*(\d+)/";
    $regex3 = "/\/N\s+(\d+)/";

    if(preg_match_all($regex, $content, $matches))
        $count = max($matches);

    return $count[0];
}

echo numeroPaginasPdf("docs/file1.pdf");
?>

--- Updating ---

Error displayed:

inserir a descrição da imagem aqui

  • Which error is displayed?

  • If it can be out of php: pdfinfo docs/file1.pdf | grep Pages

  • 1

    @Filipel.Constant put the error in the original post. ;)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.