function pdf_to_image_count_pages in PDF to ImageField 7.3
Same name and namespace in other branches
- 7.2 pdf_to_image.module \pdf_to_image_count_pages()
Use imagemagick routine to count the number of pages in a given PDF.
1 call to pdf_to_image_count_pages()
- pdf_to_image_generate_process in ./
pdf_to_image.module - Processing pdf file creation.
File
- ./
pdf_to_image.module, line 682 - Generates thumbnail image(s) from an uploaded PDF.
Code
function pdf_to_image_count_pages($filepath) {
// Assume the 'identify' binary lives next to the 'convert' binary.
$convert_path = variable_get('imagemagick_convert', '/usr/bin/convert');
$identify_path = dirname($convert_path) . '/identify';
// Identify renders every page in the pdf to count the number of pages which
// can be a problem (server timeout) when processing a pdf with many pages.
// The better command commented because it working very slow.
// "{$identify_path} -format %n " . escapeshellarg($fpath) . ' 2> /dev/null';.
// Apparently this method is even faster, from
// http://drupal.org/node/1537658
// Though we'll only use it if ghostscript is present and configured.
$gs_path = variable_get('gs_path', '/usr/local/bin/gs');
if (is_executable($gs_path)) {
$command = "{$gs_path} -q -dNODISPLAY -c \"(" . trim(pdf_to_image_escapeshellarg($filepath), "'") . ") (r) file runpdfbegin pdfpagecount = quit\"";
}
else {
// This one instead asks for more pages than the document has, then reads
// the last page that the document actually returned.
$command = "{$identify_path} " . pdf_to_image_escapeshellarg($filepath) . '[9999] | grep "Requested FirstPage" | cut -d : -f2';
}
$count = pdf_to_image_shell_exec($command);
$count = (int) trim($count);
if ($count == 0) {
watchdog('pdf_to_image', 'Attempting to count the number of pages in PDF returned zero. This may be a currupt PDF, or the imagemagick tool may not be installed or working right. Please check the status page.', array(), WATCHDOG_ERROR);
}
return $count;
}