duanhan5230 2015-11-09 09:16
浏览 57

使用Yii pdfGrid扩展名以纵向方式另存为PDF

Good Afternoon

I am using the pdfGrid extension in Yii in which i am using the class EPDFGrid..

I am really confused on how to make the orientation in PORTRAIT mode. currently the rendered PDF file is in Landscape.. i tried changing this line public $orientation = 'L'; to 'P' but it did nothing.. i followed it here.. http://www.yiiframework.com/extension/pdf-grid/ is there an option in config that dictates the orientation into PORTRAIT.?

can anybody help me..

this is the code in my EPDFGrid.php

<?php    
Yii::import('zii.widgets.grid.CDataColumn');
Yii::import('ext.pdfGrid.fpdf.PDF');

class EPDFGrid extends CWidget {

    private $_debug = false;
    protected $_pdf;
    protected $_fill = false;
    protected $_columnWidths = array();
    protected $_visibleColumns = 0;
    public $dataProvider;
    public $fileName;
    public $config = array();
    public $columns = array();
    public $labels = array();
    public $orientation = 'L';

    public $showTableOnEmpty = true;    

    public $nullDisplay = ' ';           
    public $emptyText; 
    public $hideHeader = false;

    public function init() {
        if ($this->columns === array()) {

            if ($this->dataProvider instanceof CActiveDataProvider)
                $this->columns = $this->dataProvider->model->attributeNames();
            else if ($this->dataProvider instanceof IDataProvider) {
                // use the keys of the first row of data as the default columns
                $data = $this->dataProvider->getData();
                if (isset($data[0]) && is_array($data[0]))
                    $this->columns = array_keys($data[0]);
            }
        }
        $id = $this->getId();
        foreach ($this->columns as $i => $column) {
            if (is_string($column))
                $column = $this->createDataColumn($column);
            else {
                if (!isset($column['class']))
                    $column['class'] = 'CDataColumn';
                $column = Yii::createComponent($column, $this);
            }
            if (!$column->visible) {
                unset($this->columns[$i]);
                continue;
            }
            $this->_visibleColumns++;
            if ($column->id === null)
                $column->id = $id . '_c' . $i;
            $this->columns[$i] = $column;
        }

        $default = array(
            'pdfSize' => 'A4',
            'title' => '',
            'subTitle' => '',
            'headTitle' => '',
            'amount' => '',
            'tableWidth' => 275,
            'rowHeight' => 6,
            'colAligns' => null,
            'colWidths' => null,
            'showLogo' => false,
            'imagePath' => YiiBase::getPathOfAlias('webroot') . '/images/logo.jpg',
            'headerDetails' => false,
        );

        $this->config = array_merge($default, $this->config);

        $this->_pdf = new PDF('L', 'mm', $this->config['pdfSize']);
        $this->_pdf->title = $this->config['title'];
        $this->_pdf->subTitle = $this->config['subTitle'];
        $this->_pdf->headTitle = $this->config['headTitle'];
        $this->_pdf->amount = $this->config['amount'];
        $this->_pdf->tableWidth = $this->config['tableWidth'];
        $this->_pdf->rowHeight = $this->config['rowHeight'];
        $this->_pdf->imagePath = $this->config['imagePath'];
        $this->_pdf->showLogo = $this->config['showLogo'];
        $this->_pdf->headerDetails = $this->config['headerDetails'];
        $this->_pdf->SetAligns($this->config['colAligns']);
        $this->_pdf->SetFont('Arial', 'B', 10);
        $this->_pdf->SetLineWidth(0.5);
        $this->_columnWidths = $this->_calcWidths();
        $this->_pdf->SetWidths($this->_columnWidths);
        $this->_pdf->AliasNbPages();
        $this->_pdf->AddPage();

        foreach ($this->columns as $column)
            $column->init();

        $this->renderItems();
    }

   protected function createDataColumn($text) {
        if (!preg_match('/^([\w\.]+)(:(\w*))?(:(.*))?$/', $text, $matches))
            throw new CException(Yii::t('zii', 'The column must be specified in the format of "Name:Type:Label",
                where "Type" and "Label" are optional.'));
        $column = new CDataColumn($this);
        $column->name = $matches[1];
        if (isset($matches[3]) && $matches[3] !== '')
            $column->type = $matches[3];
        if (isset($matches[5]))
            $column->header = $matches[5];
        return $column;
    }

    protected function renderItems() {
        if ($this->dataProvider->getItemCount() > 0 || $this->showTableOnEmpty) {
            $this->renderTableHeader();
            $this->renderTableBody();
        }
        else
            $this->_renderEmptyText();

        if ($this->_debug)
            Yii::app()->end();
        else {
            // $this->_pdf->Output($this->fileName . ' (' . date('Y-m-d') . ').pdf', 'D');
            $this->_pdf->Output($this->fileName . '.pdf', 'D');
            exit();
        }
    }

    protected function renderTableHeader() {
        if (!$this->hideHeader) {
            // Colores y fuente en negrita
            $this->_pdf->SetFillColor(245, 185, 120);
            $this->_pdf->SetTextColor(0);
            $this->_pdf->SetBold();

            $rowHeader = array();
            if ($this->labels != array()) {
                $rowHeader = $this->labels;
            } else {
                foreach ($this->columns as $i => $column) {
                    if ($column->name == 'Id') {
                        $rowHeader[] = strtoupper($column->name);
                    } else {
                        $rowHeader[] = $column->name;    
                    }                    
                    // $rowHeader[] = $column->grid->dataProvider->model->getAttributeLabel($column->name);
                    //$this->_pdf->Cell($this->_columnWidths[$i],$this->headerHeight,$data,0,0,'C',true);
                }
            }
            $this->_pdf->Row($rowHeader, array('fill' => true, 'header' => true));
        }
    }

    protected function renderTableBody() {
        $data = $this->dataProvider->getData();
        $n = count($data);

        // Restauraci�n de colores y fuentes
        $this->_pdf->SetFillColor(255, 242, 208);
        $this->_pdf->SetTextColor(0);
        $this->_pdf->SetFont('');
        if ($n > 0) {
            for ($row = 0; $row < $n; ++$row)
                $this->renderTableRow($row);
        }
        else
            $this->_renderEmptyText();
    }

    protected function renderTableRow($row) {
        //var_dump($this->dataProvider);
        $rowData = array();
        foreach ($this->columns as $i => $column) {
            $data = $this->dataProvider->data[$row];

            if ($column->value !== null)
                $value = $column->evaluateExpression($column->value, array('data' => $data, 'row' => $row));
            else if ($column->name !== null)
                $value = CHtml::value($data, $column->name);

//          $rowData[] = $value===null ? $this->nullDisplay : $this->_formatString($value); 
            $rowData[] = $value === null ? $this->nullDisplay : utf8_decode($value);
        }
        $this->_pdf->Row($rowData, array('fill' => $this->_fill));
        $this->_fill = !$this->_fill;
    }

    protected function _renderEmptyText() {
        $emptyText = $this->emptyText === null ? Yii::t('zii', 'No results found.') : $this->emptyText;
        $this->_pdf->Cell(array_sum($this->_columnWidths), $this->config['rowHeight'], $emptyText, 0, 0, 'L');
    }

    protected function _calcWidths() {
        $widths = array();
        $params = $this->config['colWidths'];
        $visibleCols = $this->_visibleColumns;

        if (!$params) {

            $w = $this->_pdf->tableWidth / $visibleCols;
            for ($i = 0; $i < $visibleCols; $i++)
                $widths[] = $w;
        } else if (is_array($params)) {  
            if (count($params) > $visibleCols)
                throw new Exception('La cantidad de parametros supera a las columnas visibles');
            if (array_sum($params) > $this->_pdf->tableWidth)
                throw new Exception('La suma de los parametros supera a la longitud max de la tabla');

            $nulls = 0; 
            $confWidth = 0;
            for ($i = 0; $i < $visibleCols; $i++) {
                if (empty($params[$i]))
                    $nulls++;
                else
                    $confWidth += $params[$i];
            }

            $w = $nulls ? ($this->_pdf->tableWidth - $confWidth) / $nulls : 0;   
            for ($i = 0; $i < $visibleCols; $i++) {
                $widths[] = empty($params[$i]) ? $w : $params[$i];
            }
        }
        else
            throw new Exception('El parametro $config[widths] debe ser un array');    
        return $widths;
    }

    protected function _formatString($string) {
        $string = strtolower(utf8_decode($string));
        return ucwords($string);
    }    

    protected function _combineColumns($print = '', $config = array()) {
        $default = array(
            'from' => 0,
            'to' => $this->_visibleColumns - 1,
            'border' => 0,
            'align' => 'L',
            'fill' => $this->_fill,
            'ln' => 1,
        );

        $config = array_merge($default, $config);

        $b = $this->$config['border'];
        $a = $this->$config['align'];
        $f = $this->$config['fill'];
        $ln = $this->$config['ln'];

        $w = 0;
        for ($i = $this->$config['from']; $i <= $this->$config['to']; $i++) {
            $w += $this->_columnWidths[$i];
        }

        $this->_pdf->Cell($w, $this->config['rowHeight'], $print, $b, $ln, $a, $f);
        if ($f)
            $this->_fill = !$this->_fill;
    }

}

this is the generated pdf file.

<?php
    $data_provider = $model->viewEmployees($search, $from, $to);

    $data_provider->pagination = false;

$this->widget('ext.pdfGrid.EPDFGrid', array(
        'id' => 'employee-pdf',
        'fileName' => 'Employees',
        'dataProvider' => $model->viewEmployees($search, $from, $to),
        'columns' => array(                
            array('name' => 'ID Number','value' => '$data->company_id', 'htmlOptions'=>array('width'=>'10%'),),                
            array('name' => 'Name', 'header' => 'Name', 'value' => '$data->getNameWithMiddleInitial()', 'htmlOptions' => array('width'=>'10%')),                         
            array('name' => 'Date Employed', 'value' => '$data->date_employed' ,'htmlOptions'=>array('width'=>'10%')),                
        ),
        'config' => array(
            'title' => 'Sprasia Philippines Information Management System',
            'subTitle' => 'List of Employees',
            'headerDetails' => true,
            'showLogo' => true,
            'colAligns' => array('C', 'C', 'C'),
        ),
    ));
?>

please help..

  • 写回答

1条回答 默认 最新

  • dozug64282 2015-11-10 03:05
    关注

    so silly of me.. i have found it. in this line..

     $this->_pdf->AddPage();
    

    i indicated P for portrait.. in which i have solved it by using this

     $this->_pdf->AddPage('P');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。