From 0ff39c83d38ce538a9f5dba53eca0fa9cb16d9e6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:45:49 +0200 Subject: Adding upstream version 0.10.2+dfsg1. Signed-off-by: Daniel Baumann --- library/Pdfexport/ProvidedHook/Pdfexport.php | 151 +++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 library/Pdfexport/ProvidedHook/Pdfexport.php (limited to 'library/Pdfexport/ProvidedHook') diff --git a/library/Pdfexport/ProvidedHook/Pdfexport.php b/library/Pdfexport/ProvidedHook/Pdfexport.php new file mode 100644 index 0000000..113eff0 --- /dev/null +++ b/library/Pdfexport/ProvidedHook/Pdfexport.php @@ -0,0 +1,151 @@ +isSupported()) { + throw new Exception( + sprintf("Can't export: %s does not support exporting PDFs", get_class($pdfexport)) + ); + } + } + + if (! $pdfexport) { + throw new Exception("Can't export: No module found which provides PDF export"); + } + + return $pdfexport; + } + + public static function getBinary() + { + return Config::module('pdfexport')->get('chrome', 'binary', '/usr/bin/google-chrome'); + } + + public static function getForceTempStorage() + { + return (bool) Config::module('pdfexport')->get('chrome', 'force_temp_storage', '0'); + } + + public static function getHost() + { + return Config::module('pdfexport')->get('chrome', 'host'); + } + + public static function getPort() + { + return Config::module('pdfexport')->get('chrome', 'port', 9222); + } + + public function isSupported() + { + try { + return $this->chrome()->getVersion() >= 59; + } catch (Exception $e) { + return false; + } + } + + public function htmlToPdf($html) + { + // Keep reference to the chrome object because it is using temp files which are automatically removed when + // the object is destructed + $chrome = $this->chrome(); + + $pdf = $chrome->fromHtml($html, static::getForceTempStorage())->toPdf(); + + if ($html instanceof PrintableHtmlDocument && ($coverPage = $html->getCoverPage()) !== null) { + $coverPagePdf = $chrome + ->fromHtml( + (new PrintableHtmlDocument()) + ->add($coverPage) + ->addAttributes($html->getAttributes()) + ->removeMargins(), + static::getForceTempStorage() + ) + ->toPdf(); + + $merger = new Merger(new TcpdiDriver()); + $merger->addRaw($coverPagePdf); + $merger->addRaw($pdf); + + $pdf = $merger->merge(); + } + + return $pdf; + } + + public function streamPdfFromHtml($html, $filename) + { + $filename = basename($filename, '.pdf') . '.pdf'; + + // Keep reference to the chrome object because it is using temp files which are automatically removed when + // the object is destructed + $chrome = $this->chrome(); + + $pdf = $chrome->fromHtml($html, static::getForceTempStorage())->toPdf(); + + if ($html instanceof PrintableHtmlDocument && ($coverPage = $html->getCoverPage()) !== null) { + $coverPagePdf = $chrome + ->fromHtml( + (new PrintableHtmlDocument()) + ->add($coverPage) + ->addAttributes($html->getAttributes()) + ->removeMargins(), + static::getForceTempStorage() + ) + ->toPdf(); + + $merger = new Merger(new TcpdiDriver()); + $merger->addRaw($coverPagePdf); + $merger->addRaw($pdf); + + $pdf = $merger->merge(); + } + + Icinga::app()->getResponse() + ->setHeader('Content-Type', 'application/pdf', true) + ->setHeader('Content-Disposition', "inline; filename=\"$filename\"", true) + ->setBody($pdf) + ->sendResponse(); + + exit; + } + + /** + * Create an instance of HeadlessChrome from configuration + * + * @return HeadlessChrome + */ + protected function chrome() + { + $chrome = new HeadlessChrome(); + $chrome->setBinary(static::getBinary()); + + if (($host = static::getHost()) !== null) { + $chrome->setRemote($host, static::getPort()); + } + + return $chrome; + } +} -- cgit v1.2.3