PHP retrieves the number of PDF file pages

Source: Internet
Author: User
Provides various official and user-released code examples and code reference. You are welcome to exchange and learn about this function, although it seems to be of little use, but it can still be used in some cases. If you are creating your own function library, you can add it to favorites.


Tip: this function is not my original one. The original address is called here.
The following code is provided: /**
* Function for getting the number of PDF file pages
* The file should be readable to the current user (in linux)
* @ Param [string] $ path [file path]
* @ Return [array] [the first digit of the array indicates success or not, and the second digit indicates prompt information]
*/
Function getPdfPages ($ path ){
If (! File_exists ($ path) return array (false, "file \" {$ path} \ "does not exist! ");
If (! Is_readable ($ path) return array (false, "file \" {$ path} \ "unreadable! ");
// Open the file
$ Fp = @ fopen ($ path, "r ");
If (! $ Fp ){
Return array (false, "open file \" {$ path} \ "failed ");
} Else {
$ Max = 0;
While (! Feof ($ fp )){
$ Line = fgets ($ fp, 255 );
If (preg_match ('/\/Count [0-9] +/', $ line, $ matches )){
Preg_match ('/[0-9] +/', $ matches [0], $ matches2 );
If ($ max <$ matches2 [0]) $ max = $ matches2 [0];
}
}
Fclose ($ fp );
// Number of returned pages
Return array (true, $ max );
}
}
/**
* Test code
*/
$ Results = getPdfPages ("demoages ");
If ($ results [0]) {
// Put the processing code after successful reading here
} Else {
// Put the failed processing code here
}
?>
In terms of performance, it takes about 1 second to read a 600-page file on my ubuntu machine.

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.