2014-11-21 5 views
1

Yii 프레임 워크를 사용하여 mPDF 라이브러리에서 pdf를 생성 중입니다. 이 PDF의 경우 서버에서 생성 된 차트를 추가해야하므로 pChart 라이브러리를 사용하여 차트를 생성합니다. pChart 속성을 PDF의보다 쉬운 방법으로 설정하는 간단한 객체 클래스를 만들었습니다. 치명적인 오류 : pDraw 클래스를 다시 선언 할 수 없습니다.

는 클래스 객체입니다

<?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)를 참조하십시오.

답변

1

include 대신 include_once을 사용하십시오.

include_once('pDraw.class.php'); 
include_once('pImage.class.php'); 
include_once('pData.class.php'); 
+0

트릭을 만들었습니다. 지금은 너무 바보 같아서, 지금은 아직 초보자 프로그래머입니다. – nosthertus