diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:46:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:46:43 +0000 |
commit | 3e02d5aff85babc3ffbfcf52313f2108e313aa23 (patch) | |
tree | b01f3923360c20a6a504aff42d45670c58af3ec5 /library/Icinga/File/Pdf.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-3e02d5aff85babc3ffbfcf52313f2108e313aa23.tar.xz icingaweb2-3e02d5aff85babc3ffbfcf52313f2108e313aa23.zip |
Adding upstream version 2.12.1.upstream/2.12.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Icinga/File/Pdf.php')
-rw-r--r-- | library/Icinga/File/Pdf.php | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/library/Icinga/File/Pdf.php b/library/Icinga/File/Pdf.php new file mode 100644 index 0000000..1b78424 --- /dev/null +++ b/library/Icinga/File/Pdf.php @@ -0,0 +1,81 @@ +<?php +/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */ + +namespace Icinga\File; + +use Dompdf\Dompdf; +use Dompdf\Options; +use Icinga\Application\Icinga; +use Icinga\Exception\ProgrammingError; +use Icinga\Util\Environment; +use Icinga\Web\Hook; +use Icinga\Web\Url; + +class Pdf +{ + protected function assertNoHeadersSent() + { + if (headers_sent()) { + throw new ProgrammingError( + 'Could not send pdf-response, content already written to output.' + ); + } + } + + public function renderControllerAction($controller) + { + $this->assertNoHeadersSent(); + + Environment::raiseMemoryLimit('512M'); + Environment::raiseExecutionTime(300); + + $viewRenderer = $controller->getHelper('viewRenderer'); + $viewRenderer->postDispatch(); + + $layoutHelper = $controller->getHelper('layout'); + $oldLayout = $layoutHelper->getLayout(); + $layout = $layoutHelper->setLayout('pdf'); + + $layout->content = $controller->getResponse(); + $html = $layout->render(); + + // Restore previous layout and reset content, to properly show errors + $controller->getResponse()->clearBody($viewRenderer->getResponseSegment()); + $layoutHelper->setLayout($oldLayout); + + $imgDir = Url::fromPath('img'); + $html = preg_replace( + '~src="' . $imgDir . '/~', + 'src="' . Icinga::app()->getBootstrapDirectory() . '/img/', + $html + ); + + $request = $controller->getRequest(); + + if (Hook::has('Pdfexport')) { + $pdfexport = Hook::first('Pdfexport'); + $pdfexport->streamPdfFromHtml($html, sprintf( + '%s-%s-%d', + $request->getControllerName(), + $request->getActionName(), + time() + )); + + return; + } + + $options = new Options(); + $options->set('defaultPaperSize', 'A4'); + $dompdf = new Dompdf($options); + $dompdf->loadHtml($html); + $dompdf->render(); + $dompdf->stream( + sprintf( + '%s-%s-%d', + $request->getControllerName(), + $request->getActionName(), + time() + ) + ); + } +} |