Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions library/Icinga/Application/Hook/PdfexportHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +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
{
/**
* Get the first hook
*
* @return static
*/
public static function first()
Comment thread
Al2Klimov marked this conversation as resolved.
{
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);
}
6 changes: 4 additions & 2 deletions library/Icinga/Common/PdfExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Icinga\Common;

use Icinga\Application\Hook\PdfexportHook;
use Icinga\Application\Icinga;
use Icinga\Date\DateFormatter;
use Icinga\Exception\ConfigurationError;
Expand All @@ -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()
{
Expand Down Expand Up @@ -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
Expand Down
Loading