From 3e02d5aff85babc3ffbfcf52313f2108e313aa23 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 13:46:43 +0200 Subject: Adding upstream version 2.12.1. Signed-off-by: Daniel Baumann --- library/Icinga/File/Pdf.php | 81 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 library/Icinga/File/Pdf.php (limited to 'library/Icinga/File/Pdf.php') 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 @@ +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() + ) + ); + } +} -- cgit v1.2.3