Merge pull request #3529 from Icinga/feature/pdfexporthook
Introduce PdfexportHook
This commit is contained in:
commit
99bf33da33
|
@ -0,0 +1,25 @@
|
||||||
|
<?php
|
||||||
|
/* Icinga Web 2 | (c) 2018 Icinga Development Team | GPLv2+ */
|
||||||
|
|
||||||
|
namespace Icinga\Application\Hook;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base class for the PDF Export Hook
|
||||||
|
*/
|
||||||
|
abstract class PdfexportHook
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Get whether PDF export is supported
|
||||||
|
*
|
||||||
|
* @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
|
||||||
|
*/
|
||||||
|
abstract public function streamPdfFromHtml($html, $filename);
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ use Dompdf\Dompdf;
|
||||||
use Dompdf\Options;
|
use Dompdf\Options;
|
||||||
use Icinga\Application\Icinga;
|
use Icinga\Application\Icinga;
|
||||||
use Icinga\Exception\ProgrammingError;
|
use Icinga\Exception\ProgrammingError;
|
||||||
|
use Icinga\Web\Hook;
|
||||||
use Icinga\Web\Url;
|
use Icinga\Web\Url;
|
||||||
|
|
||||||
call_user_func(function () {
|
call_user_func(function () {
|
||||||
|
@ -61,12 +62,29 @@ class Pdf
|
||||||
'src="' . Icinga::app()->getBootstrapDirectory() . '/img/',
|
'src="' . Icinga::app()->getBootstrapDirectory() . '/img/',
|
||||||
$html
|
$html
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$request = $controller->getRequest();
|
||||||
|
|
||||||
|
if (Hook::has('Pdfexport')) {
|
||||||
|
$pdfexport = Hook::first('Pdfexport');
|
||||||
|
|
||||||
|
if ($pdfexport->isSupported()) {
|
||||||
|
$pdfexport->streamPdfFromHtml($html, sprintf(
|
||||||
|
'%s-%s-%d',
|
||||||
|
$request->getControllerName(),
|
||||||
|
$request->getActionName(),
|
||||||
|
time()
|
||||||
|
));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$options = new Options();
|
$options = new Options();
|
||||||
$options->set('defaultPaperSize', 'A4');
|
$options->set('defaultPaperSize', 'A4');
|
||||||
$dompdf = new Dompdf($options);
|
$dompdf = new Dompdf($options);
|
||||||
$dompdf->loadHtml($html);
|
$dompdf->loadHtml($html);
|
||||||
$dompdf->render();
|
$dompdf->render();
|
||||||
$request = $controller->getRequest();
|
|
||||||
$dompdf->stream(
|
$dompdf->stream(
|
||||||
sprintf(
|
sprintf(
|
||||||
'%s-%s-%d',
|
'%s-%s-%d',
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
namespace Icinga\Web\Widget\Tabextension;
|
namespace Icinga\Web\Widget\Tabextension;
|
||||||
|
|
||||||
use Icinga\Application\Platform;
|
use Icinga\Application\Platform;
|
||||||
|
use Icinga\Application\Hook;
|
||||||
use Icinga\Web\Url;
|
use Icinga\Web\Url;
|
||||||
use Icinga\Web\Widget\Tab;
|
use Icinga\Web\Widget\Tab;
|
||||||
use Icinga\Web\Widget\Tabs;
|
use Icinga\Web\Widget\Tabs;
|
||||||
|
@ -81,7 +82,13 @@ class OutputFormat implements Tabextension
|
||||||
{
|
{
|
||||||
$supportedTypes = array();
|
$supportedTypes = array();
|
||||||
|
|
||||||
if (Platform::extensionLoaded('gd')) {
|
$pdfexport = false;
|
||||||
|
|
||||||
|
if (Hook::has('Pdfexport')) {
|
||||||
|
$pdfexport = Hook::first('Pdfexport')->isSupported();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($pdfexport || Platform::extensionLoaded('gd')) {
|
||||||
$supportedTypes[self::TYPE_PDF] = array(
|
$supportedTypes[self::TYPE_PDF] = array(
|
||||||
'name' => 'pdf',
|
'name' => 'pdf',
|
||||||
'label' => 'PDF',
|
'label' => 'PDF',
|
||||||
|
|
Loading…
Reference in New Issue