ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
PDF.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
25 
29 class PDF extends General implements Extractor
30 {
31  public function getResolution(): int
32  {
33  return 96;
34  }
35 
36  public function readImage(\Imagick $img, Stream $stream, PagesToExtract $definition): \Imagick
37  {
38  // Using ghostscript to extract pages from PDFs is faster and more reliable. If ghostscript is available, use it.
39  if (defined('PATH_TO_GHOSTSCRIPT') && PATH_TO_GHOSTSCRIPT !== "") {
40  // extract one single page as image using ghostscript in cli and add it to the imagick object
41  $file_path = $stream->getMetadata()['uri'];
42  $start_page = 1;
43 
44  $cmd = PATH_TO_GHOSTSCRIPT . " -q -dNODISPLAY -dNOSAFER -c \"($file_path) (r) file runpdfbegin pdfpagecount = quit\";";
45  $pages_in_file = (int) shell_exec($cmd);
46 
47  $end_page = min($pages_in_file, $definition->getMaxPages());
48  for ($i = $start_page; $i <= $end_page; $i++) {
49  // run ghostscript in cli and return the image to stdout
50  $cmd = PATH_TO_GHOSTSCRIPT . " -dNOPAUSE -sDEVICE=png16m -r"
51  . $this->getResolution()
52  . " -dFirstPage=" . $i
53  . " -dLastPage=" . $i .
54  " -sOutputFile=- -q " . escapeshellarg((string) $file_path);
55  $pages_in_file = shell_exec($cmd);
56  $page = new \Imagick();
57  $page->readImageBlob($pages_in_file);
58  $img->addImage($page);
59  }
60 
61  return $img;
62  }
63  // otherwise we try to extract the pages using the Imagick API
64  $resource = $stream->detach();
65  fseek($resource, 0);
66  $img->readImageFile($resource);
67  return $img;
68  }
69 
70 }
readImage(\Imagick $img, Stream $stream, PagesToExtract $definition)
Definition: PDF.php:36