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;
|
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
|
|
|
require_once 'dompdf/autoload.inc.php';
|
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();
|
|
|
|
ini_set('memory_limit', '384M');
|
|
|
|
ini_set('max_execution_time', 300);
|
2015-02-11 15:14:40 +01:00
|
|
|
$viewRenderer = $controller->getHelper('viewRenderer');
|
|
|
|
$controller->render(
|
|
|
|
$viewRenderer->getScriptAction(),
|
|
|
|
$viewRenderer->getResponseSegment(),
|
|
|
|
$viewRenderer->getNoController()
|
|
|
|
);
|
2014-03-06 12:21:11 +01:00
|
|
|
$layout = $controller->getHelper('layout')->setLayout('pdf');
|
|
|
|
$layout->content = $controller->getResponse();
|
|
|
|
$html = $layout->render();
|
|
|
|
$imgDir = Url::fromPath('img');
|
2014-11-12 17:14:09 +01:00
|
|
|
$html = preg_replace('~src="' . $imgDir . '/~', 'src="' . Icinga::app()->getBootstrapDirectory() . '/img/', $html);
|
2016-11-10 14:59:18 +01:00
|
|
|
$options = new Options();
|
|
|
|
$options->set('defaultPaperSize', 'A4');
|
|
|
|
$dompdf = new Dompdf($options);
|
|
|
|
$dompdf->loadHtml($html);
|
|
|
|
$dompdf->render();
|
2015-02-11 15:14:40 +01:00
|
|
|
$request = $controller->getRequest();
|
2016-11-10 14:59:18 +01:00
|
|
|
$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
|
|
|
}
|