diff --git a/library/Icinga/Application/Hook/PdfexportHook.php b/library/Icinga/Application/Hook/PdfexportHook.php index 0ae0c530df..3716ce88ac 100644 --- a/library/Icinga/Application/Hook/PdfexportHook.php +++ b/library/Icinga/Application/Hook/PdfexportHook.php @@ -5,30 +5,65 @@ namespace Icinga\Application\Hook; +use Icinga\Application\Hook; +use Icinga\Application\Logger; +use Icinga\Exception\IcingaException; +use ipl\Html\ValidHtml; +use RuntimeException; +use Throwable; + /** * Base class for the PDF Export Hook */ abstract class PdfexportHook { - use HookEssentials; - - final protected static function getHookName(): string + /** + * Get the first hook that supports PDF export + * + * @return static + */ + public static function first() { - return 'Pdfexport'; + foreach (Hook::all('Pdfexport') as $exporter) { + try { + if ($exporter->isSupported()) { + return $exporter; + } + } catch (Throwable $e) { + Logger::error( + "PDF exporter reported an error during support check: %s\n%s", + $e, + IcingaException::getConfidentialTraceAsString($e), + ); + } + } + + throw new RuntimeException('No supported PDF exporter available'); } /** * Get whether PDF export is supported * - * @return bool + * @return bool */ abstract public function isSupported(); /** * Render the specified HTML to PDF and stream it to the client * - * @param string $html The HTML to render to PDF - * @param string $filename The filename for the generated PDF + * @param ValidHtml $html The HTML to render to PDF + * @param string $filename The filename for the generated PDF + * + * @return never */ abstract public function streamPdfFromHtml($html, $filename); + + /** + * Render the specified HTML to PDF and return the PDF document as a string + * + * @param ValidHtml $html The HTML to render to PDF + * + * @return string + */ + abstract public function htmlToPdf($html); } diff --git a/library/Icinga/Common/PdfExport.php b/library/Icinga/Common/PdfExport.php index ed49fafb1e..4093a7ab63 100644 --- a/library/Icinga/Common/PdfExport.php +++ b/library/Icinga/Common/PdfExport.php @@ -5,6 +5,7 @@ namespace Icinga\Common; +use Icinga\Application\Hook\PdfexportHook; use Icinga\Application\Icinga; use Icinga\Date\DateFormatter; use Icinga\Exception\ConfigurationError; @@ -26,7 +27,8 @@ trait PdfExport * Export the requested action to PDF and send it * * @return never - * @throws ConfigurationError If the pdfexport module is not available + * + * @throws ConfigurationError If no pdfexport module is available */ protected function sendAsPdf() { @@ -71,7 +73,7 @@ protected function sendAsPdf() $doc->getAttributes()->add('class', 'icinga-module module-' . $moduleName); } - \Icinga\Module\Pdfexport\ProvidedHook\Pdfexport::first()->streamPdfFromHtml($doc, sprintf( + PdfexportHook::first()->streamPdfFromHtml($doc, sprintf( '%s-%s', $this->view->title ?: $this->getRequest()->getActionName(), $time