简体   繁体   中英

Php count number of pages on PDF file upon upload prior to saving file

I have a function that uploads a file into a web storage and prior to saving the file on the storage system if the file is a pdf file i would like to determine how many pages a pdf file has.

Currently i have the following:

    $pdftext = file_get_contents($path);
    $num = preg_match_all("/\/Page\W/", $pdftext, $dummy);
    return $num;

Where $path is the temporary path that i use with fopen to open the document

This function works at times but is not reliable. I know theres also this function

exec('/usr/bin/pdfinfo '.$pdf_file.' | awk \'/Pages/ {print $2}\'', $output);

But this requires the file to donwloaded on the server. Any ideas or suggestions to accomplish this?

PHP is a server-side language, meaning all processing happens on your server. There's no way for PHP to determine details of a file on the client side, it has no knowledge of it neither the required access to it.

So the answer to your question as it is now is: It's not possible. But you probably have a goal in mind why you want to check this, sharing this goal might help to get more constructive answers/suggestions.

As Oldskool already explained this is not possible with PHP on the client side. You would have to upload the PDF file to the server and then determine the amount of pages. There are libraries and command line tools that could accomplish this.

In case you don't want to upload the PDF file to the server (which seems to be the case here) you could use the pdf.js library. Now the client is able to determine the amount of pages in a PDF document on its own.

PDFJS.getDocument(data).then(function (doc) {
    var numPages = doc.numPages;
}

There are other libraries as well but I'm not certain about their browser support ( http://www.electronmedia.in/wp/pdf-page-count-javascript/ )

Now you just submit the amount of pages from javascript to your php file that needs this information. In order to achive this you simply use ajax. In case you don't know ajax, just google it there are enough examples out there.

As a side note; Always remember to not trust the client. The client is able to modify the page count and send a completely different one.

For those of you running linux servers this actually is possible. You need the pdfinfo extension installed and using the function

  $pages = exec('/usr/bin/pdfinfo '.$pdf_file.' | awk \'/Pages/ {print $2}\'', $output);

outputs the correct page number where $pdf_file is the temporary path on the server upon upload.

The reason it wasnt working for me was because i didnt have the PDFinfo installed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM