2013-06-07 11:44:37 +02:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
|
2013-06-07 11:44:37 +02:00
|
|
|
|
2013-07-12 11:58:58 +02:00
|
|
|
namespace Icinga\File;
|
2013-06-07 11:44:37 +02:00
|
|
|
|
2016-11-10 14:59:18 +01:00
|
|
|
use Dompdf\Dompdf;
|
|
|
|
use Dompdf\Options;
|
2014-03-06 12:21:11 +01:00
|
|
|
use Icinga\Application\Icinga;
|
|
|
|
use Icinga\Exception\ProgrammingError;
|
2018-12-05 15:31:15 +01:00
|
|
|
use Icinga\Util\Environment;
|
2018-07-18 14:16:04 +02:00
|
|
|
use Icinga\Web\Hook;
|
2016-11-10 14:59:18 +01:00
|
|
|
use Icinga\Web\Url;
|
2013-06-07 11:44:37 +02:00
|
|
|
|
2016-11-10 14:59:18 +01:00
|
|
|
class Pdf
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2014-03-06 12:21:11 +01:00
|
|
|
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();
|
2018-12-03 15:29:19 +01:00
|
|
|
|
2018-12-05 15:31:15 +01:00
|
|
|
Environment::raiseMemoryLimit('512M');
|
|
|
|
Environment::raiseExecutionTime(300);
|
2018-12-03 15:29:19 +01:00
|
|
|
|
2015-02-11 15:14:40 +01:00
|
|
|
$viewRenderer = $controller->getHelper('viewRenderer');
|
2020-01-10 14:08:21 +01:00
|
|
|
$viewRenderer->postDispatch();
|
|
|
|
|
|
|
|
$layoutHelper = $controller->getHelper('layout');
|
|
|
|
$oldLayout = $layoutHelper->getLayout();
|
|
|
|
$layout = $layoutHelper->setLayout('pdf');
|
|
|
|
|
2014-03-06 12:21:11 +01:00
|
|
|
$layout->content = $controller->getResponse();
|
|
|
|
$html = $layout->render();
|
2020-01-10 14:08:21 +01:00
|
|
|
|
|
|
|
// Restore previous layout and reset content, to properly show errors
|
|
|
|
$controller->getResponse()->clearBody($viewRenderer->getResponseSegment());
|
|
|
|
$layoutHelper->setLayout($oldLayout);
|
|
|
|
|
2014-03-06 12:21:11 +01:00
|
|
|
$imgDir = Url::fromPath('img');
|
2017-01-27 14:48:59 +01:00
|
|
|
$html = preg_replace(
|
|
|
|
'~src="' . $imgDir . '/~',
|
|
|
|
'src="' . Icinga::app()->getBootstrapDirectory() . '/img/',
|
|
|
|
$html
|
|
|
|
);
|
2018-07-18 14:16:04 +02:00
|
|
|
|
|
|
|
$request = $controller->getRequest();
|
|
|
|
|
|
|
|
if (Hook::has('Pdfexport')) {
|
|
|
|
$pdfexport = Hook::first('Pdfexport');
|
2020-01-14 10:59:01 +01:00
|
|
|
$pdfexport->streamPdfFromHtml($html, sprintf(
|
|
|
|
'%s-%s-%d',
|
|
|
|
$request->getControllerName(),
|
|
|
|
$request->getActionName(),
|
|
|
|
time()
|
|
|
|
));
|
2018-07-18 14:16:04 +02:00
|
|
|
|
2020-01-14 10:59:01 +01:00
|
|
|
return;
|
2018-07-18 14:16:04 +02:00
|
|
|
}
|
|
|
|
|
2016-11-10 14:59:18 +01:00
|
|
|
$options = new Options();
|
|
|
|
$options->set('defaultPaperSize', 'A4');
|
|
|
|
$dompdf = new Dompdf($options);
|
|
|
|
$dompdf->loadHtml($html);
|
|
|
|
$dompdf->render();
|
|
|
|
$dompdf->stream(
|
2014-03-06 12:21:11 +01:00
|
|
|
sprintf(
|
2016-11-10 14:59:18 +01:00
|
|
|
'%s-%s-%d',
|
2014-03-06 12:21:11 +01:00
|
|
|
$request->getControllerName(),
|
|
|
|
$request->getActionName(),
|
|
|
|
time()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2014-06-05 02:10:49 +02:00
|
|
|
}
|