Pdf/Charts: add initial chart support to PDFs

Problem: TCPDF had SVG support, dompdf hasn't. This patch adds a
first rudimentary PNG conversion and a sample implementation making
use of such.
This commit is contained in:
Thomas Gelf 2014-05-20 22:48:06 +00:00
parent 5e69d1d997
commit 24f2ae607f
4 changed files with 35 additions and 4 deletions

View File

@ -87,6 +87,17 @@ if (in_array($path, $special)) {
$pie->initFromRequest();
echo $pie->render();
} elseif ($path === 'png/chart.php') {
if (!array_key_exists('data', $_GET)) {
return false;
}
include __DIR__ . '/EmbeddedWeb.php';
EmbeddedWeb::start();
header('Content-Type: image/png');
$pie = new PieChart();
$pie->initFromRequest();
$pie->toPng();
} elseif (file_exists($baseDir . '/' . $path) && is_file($baseDir . '/' . $path)) {
return false;
} else {

View File

@ -31,12 +31,14 @@
namespace Icinga\Chart\Inline;
use Icinga\Chart\PieChart as PieChartRenderer;
use Imagick;
use Exception;
/**
* Draw an inline pie-chart directly from the available request parameters.
*/
class PieChart extends Inline {
public function render()
public function render($output = true)
{
$pie = new PieChartRenderer();
$pie->disableLegend();
@ -44,6 +46,23 @@ class PieChart extends Inline {
'data' => $this->data, 'colors' => $this->colors, 'labels' => $this->labels
));
$pie->setWidth($this->width)->setHeight($this->height);
if ($output) {
echo $pie->render();
} else {
return $pie->render();
}
}
public function toPng()
{
if (! class_exists('Imagick')) {
// TODO: This is quick & dirty. 404?
throw new Exception('Cannot render PNGs without Imagick');
}
$image = new Imagick();
$image->readImageBlob($this->render(false));
$image->setImageFormat('png24');
$image->resizeImage($this->width, $this->height, imagick::FILTER_LANCZOS, 1);
echo $image;
}
}

View File

@ -48,6 +48,7 @@ class Pdf extends DOMPDF
$html = $layout->render();
$imgDir = Url::fromPath('img');
$html = preg_replace('~src="' . $imgDir . '/~', 'src="' . Icinga::app()->getBootstrapDirecory() . '/img/', $html);
$html = preg_replace('~src="/svg/chart.php(.*)"~', 'class="icon" src="http://master1.com/png/chart.php$1"', $html);
$this->load_html($html);
$this->render();
$this->stream(

View File

@ -266,7 +266,7 @@ def("DOMPDF_ENABLE_PHP", false);
*
* @var bool
*/
def("DOMPDF_ENABLE_JAVASCRIPT", true);
def("DOMPDF_ENABLE_JAVASCRIPT", false;
/**
* Enable remote file access
@ -285,7 +285,7 @@ def("DOMPDF_ENABLE_JAVASCRIPT", true);
*
* @var bool
*/
def("DOMPDF_ENABLE_REMOTE", false);
def("DOMPDF_ENABLE_REMOTE", true);
/**
* The debug output log