pdfgridviewyii2yii-componentskartik-v

kartik GridView Yii2 - How To Configure the Export Configuration For PDFs (Header, Footer, Title)


I'm trying to figure out a way to modify the header for the PDFs when I do the export.

Right now the header say something like Yii2 Grid Export (PDF) Grid Export.

Here is the code I'm using to try to modify it:

            'exportConfig' => [
                GridView::PDF => [
                    'label' => 'PDF',
                    'filename' => 'Preceptors',
                    'title' => 'Preceptors',                        
                    'options' => ['title' => 'Preceptor List','author' => 'Me'],                        
                ],
                GridView::CSV => [
                    'label' => 'CSV',
                    'filename' => 'Preceptors',
                    'options' => ['title' => 'Preceptor List'],                                             
                ],
            ],

            'export' => [
                'PDF' => [
                    'options' => [
                        'title' => 'Preceptors',
                        'subject' => 'Preceptors',
                        'author' => 'NYCSPrep CIMS',
                        'keywords' => 'NYCSPrep, preceptors, pdf'
                    ]
                ],
            ],

Solution

  • Just extend this fine kartik's grid (http://demos.krajee.com/grid) and overide run() and initExport() something like:

    namespace common\widgets;
    
    use Yii;
    use yii\web\JsExpression;
    use yii\helpers\ArrayHelper;
    
    
    class GridView extends \kartik\grid\GridView
    {
        protected function initExport() {
            ... copy original and set whatever you like
        }
    
        public function run() {
            parent::run();
            $this->initExport();        
        }
    
    }
    

    Of course set your namespace as you need it.