我正在使用mPDF
框架从Yii
库生成pdf。对于这个PDF我需要添加在服务器上生成的图表,所以我使用pChart
库来生成它们。我创建了一个简单的对象类,以便以更简单的方式为PDF设置pChart
属性。
这是类对象:
<?php
Class Chart {
protected $fontsFolder = null;
protected $data = array();
protected $resolution = array();
public $chartType = null;
public $fontSize = 18;
public $displayValues = false;
public $dirFile = null;
function __construct() {
Yii::import('application.vendors.pChart.class.*', true);
$this->fontsFolder = Yii::getPathOfALias('application.vendors.pChart.fonts');
}
// Set data points
public function data($data) {
if(!isset($data))
throw new CException('Data array missing');
if(!is_array($data))
throw new CException('Data must be an array');
$this->data[] = $data;
}
// Set label points
public function labels($labels) {
if(!isset($labels))
throw new CException('Labels data must be assgined');
if(!is_array($labels))
throw new CException('Labels data must be an array');
if(isset($this->data['labels']))
throw new CException('Labels data is already assigned');
$this->data['labels'] = $labels;
}
// Set resolution image
public function resolution($x, $y) {
if(isset($x) && isset($y)) {
if(is_array($x) && is_array($y))
throw new CException('Array to String error');
$this->resolution['x'] = $x;
$this->resolution['y'] = $y;
} else
throw new CException('Resolution data missing');
}
public function Debug() {
var_dump($this->fontsFolder, $this->data, $this->resolution);
}
// Render chart with given data
public function renderChart() {
if(!$this->data)
throw new CException('Data property must be assigned');
if(!$this->resolution)
throw new CException('Resolution property must be assigned');
if(!$this->chartType)
throw new CException('Chart type cannot be null');
if(!$this->dirFile)
throw new CException('Directory file must be assigned');
$this->render();
}
protected function render() {
switch ($this->chartType) {
case 'lineChart':
$this->lineChart();
break;
default:
throw new CEXception('"'.$this->chartType.'" is not a valid chart type');
break;
}
}
protected function lineChart() {
include('pDraw.class.php');
include('pImage.class.php');
include('pData.class.php');
$data = new pData();
foreach($this->data as $key => $value) {
if(is_int($key))
$data->addPoints($value);
}
$data->addPoints($this->data['labels'], 'labels');
$data->setAbscissa('labels');
$picture = new pImage($this->resolution['x'], $this->resolution['y'], $data);
$picture->setFontProperties(array('FontName'=>$this->fontsFolder.'/Forgotte.ttf'), $this->fontSize);
$picture->setGraphArea(60, 40, 970, 190);
$picture->drawScale(array(
'GridR'=>200,
'GridG'=>200,
'GridB'=>200,
'DrawXLines'=>false
));
$picture->drawLineChart(array('DisplayValues'=>$this->displayValues));
$picture->render($this->dirFile);
}
}
?>
以下是我用PDF设置实例化对象的方法:
<?php
Class SeguimientoController extends Controller {
// public $layout = '//layouts/column2';
public function actionIndex() {
// Cargar libreria pdf y estilos
$mPDF = Yii::app()->ePdf->mpdf();
$mPDF->debug = true;
$mPDF->showImageErrors = true;
$stylesheet = file_get_contents(Yii::getPathOfAlias('webroot.css.reporte') . '/main.css');
$mPDF->WriteHTML($stylesheet, 1);
// Generate first chart
$chart = new Chart;
$chart->data(array(1,2,3,4,5));
$chart->Labels(array('asd', 'dead', 'eads', 'daed', 'fasd'));
$chart->resolution(1000, 200);
$chart->chartType = 'lineChart';
$chart->displayValues = true;
$chart->dirFile = Yii::getPathOfAlias('webroot.images').'/chart.png';
$chart->renderChart();
$mPDF->WriteHTml(CHtml::image('/basedato1/images/chart.png', 'chart'), 2);
// Generate second chart
$chart1 = new Chart;
$chart1->data(array(1,2,3,4,5));
$chart1->Labels(array('asd', 'dead', 'eads', 'daed', 'fasd'));
$chart1->resolution(1000, 200);
$chart1->chartType = 'lineChart';
$chart1->displayValues = true;
$chart1->dirFile = Yii::getPathOfAlias('webroot.images').'/chart.png';
$chart1->renderChart();
$mPDF->WriteHTml(CHtml::image('/basedato1/images/chart.png', 'chart'), 2);
$mPDF->OutPut();
}
}
?>
通过逐步测试视图的输出,一切正常,图表被渲染并保存为临时文件,因此pdf可以收集它。但是当我必须渲染第二张图表时,我会收到一个致命的错误。
我尝试创建另一个要由静态函数访问的类,因为我认为错误可以来自同一个对象的多个实例。但同样,在第二次渲染时我得到了同样的错误。
以防万一,如果您需要知道错误的输出方式,请输入:Fatal error: Cannot redeclare class pDraw in C:\Servidor\basedato1\protected\vendors\pChart\class\pDraw.class.php on line 104
。
答案 0 :(得分:2)
使用include_once
代替include
。
include_once('pDraw.class.php');
include_once('pImage.class.php');
include_once('pData.class.php');