ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCountPDFPages.php
Go to the documentation of this file.
1<?php
2
22
30{
34 private Services $irss;
35 private bool $postscript_available = false;
36 private bool $imagick_available = false;
37
38 public function __construct()
39 {
40 global $DIC;
41 $this->irss = $DIC->resourceStorage();
42 $this->postscript_available = (defined('PATH_TO_GHOSTSCRIPT') && PATH_TO_GHOSTSCRIPT !== "");
43 $this->imagick_available = class_exists('Imagick');
44 }
45
46 public function isAvailable(): bool
47 {
48 return $this->postscript_available || $this->imagick_available;
49 }
50
52 {
53 if (!$this->postscript_available) {
54 return null;
55 }
56 $revision = $this->irss->manage()->getCurrentRevision($rid);
57 $info = $revision->getInformation();
58 if ($info->getMimeType() !== MimeType::APPLICATION__PDF) {
59 return null;
60 }
61 $consumer = $this->irss->consume()->stream($rid);
62 $path_to_pdf = $consumer->getStream()->getMetadata('uri');
63 return $this->extractAmountOfPagesByPath($path_to_pdf);
64 }
65
66 public function extractAmountOfPagesByPath(string $path_to_pdf): ?int
67 {
68 if (!$this->postscript_available && !$this->imagick_available) {
69 return null;
70 }
71
72 // first we try using Imagick
73 if ($this->imagick_available) {
74 $pages = null;
75 try {
76 $imagick = new Imagick();
77 $imagick->pingImage($path_to_pdf);
78
79 return $imagick->getNumberImages();
80 } catch (Throwable) {
81 // Imagick is not available or another error occured
82 }
83 }
84
85 if ($this->postscript_available) {
86 $arg = "-q -dNODISPLAY -dNOSAFER -c \"($path_to_pdf) (r) file runpdfbegin pdfpagecount = quit\";";
87 $return = ilShellUtil::execQuoted(PATH_TO_GHOSTSCRIPT, $arg);
88 if (!isset($return[0]) || ($pages = (int) $return[0]) === 0) {
89 return null;
90 }
91
92 return $pages;
93 }
94
95 return null;
96 }
97}
Mime type determination.
Definition: MimeType.php:30
Class ilCountPDFPages.
Services $irss
@readonly
extractAmountOfPagesByPath(string $path_to_pdf)
extractAmountOfPagesByRID(ResourceIdentification $rid)
static execQuoted(string $cmd, ?string $args=null)
$info
Definition: entry_point.php:21
global $DIC
Definition: shib_login.php:26