-
-
Save nilyang/b75e154f9a63c71afdb758f4fa53cfd8 to your computer and use it in GitHub Desktop.
Get number of pages from a PDF, using GhostScript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Return the number of pages of a PDF. | |
* | |
* Imagick::pingImage() takes too much when dealing with big files. | |
* | |
* Command to execute: | |
* | |
* gs -q -dNODISPLAY -c "(%s) (r) file runpdfbegin pdfpagecount = quit" | |
* | |
* where %s is the filename. | |
* | |
*/ | |
try { | |
$r = ''; | |
$f = array_key_exists(1, $argv) ? trim($argv[1]) : NULL; | |
if( is_null($f) || ! file_exists($f)) | |
throw new Exception('File not found or not defined', 1); | |
exec(sprintf('gs -q -dNODISPLAY -c "(%s) (r) file runpdfbegin pdfpagecount = quit"', $f), $res, $ret); | |
if(0 <> $ret) | |
$r = 'Error ' . $ret; | |
else | |
$r = (int) $res[0]; | |
} catch (Exception $e) { | |
$r = sprintf('Error (%u) %s at line %u', $e->getCode(), $e->getMessage(), $e->getLine()); | |
$r .= PHP_EOL; | |
$r .= 'Usage: ' . basename(__FILE__) . ' file.pdf'; | |
} finally { | |
print $r . PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
计算pdf页码数量