ILIAS  trunk Revision v11.0_alpha-1843-g9e1fad99175
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
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  #[\Override]
32  public function getResolution(): int
33  {
34  return 96;
35  }
36 
37  #[\Override]
38  public function readImage(\Imagick $img, Stream $stream, PagesToExtract $definition): \Imagick
39  {
40  // Using ghostscript to extract pages from PDFs is faster and more reliable. If ghostscript is available, use it.
41  if (defined('PATH_TO_GHOSTSCRIPT') && PATH_TO_GHOSTSCRIPT !== "") {
42  // extract one single page as image using ghostscript in cli and add it to the imagick object
43  $file_path = $stream->getMetadata()['uri'];
44  $start_page = 1;
45 
46  $cmd = PATH_TO_GHOSTSCRIPT . " -q -dNODISPLAY -dNOSAFER -c \"($file_path) (r) file runpdfbegin pdfpagecount = quit\";";
47  $pages_in_file = (int) shell_exec($cmd);
48 
49  $end_page = min($pages_in_file, $definition->getMaxPages());
50  for ($i = $start_page; $i <= $end_page; $i++) {
51  // run ghostscript in cli and return the image to stdout
52  $cmd = PATH_TO_GHOSTSCRIPT . " -dNOPAUSE -sDEVICE=png16m -r"
53  . $this->getResolution()
54  . " -dFirstPage=" . $i
55  . " -dLastPage=" . $i .
56  " -sOutputFile=- -q " . escapeshellarg((string) $file_path);
57  $pages_in_file = shell_exec($cmd);
58  $page = new \Imagick();
59  $page->readImageBlob($pages_in_file);
60  $img->addImage($page);
61  }
62 
63  return $img;
64  }
65  // otherwise we try to extract the pages using the Imagick API
66  $resource = $stream->detach();
67  fseek($resource, 0);
68  $img->readImageFile($resource);
69  return $img;
70  }
71 
72 }
readImage(\Imagick $img, Stream $stream, PagesToExtract $definition)
Definition: PDF.php:38