3

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'
                    ]
                ],
            ],
Andrey Matveev
  • 145
  • 1
  • 7
O2U
  • 429
  • 2
  • 8
  • 22

2 Answers2

2

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.

Vladimir
  • 445
  • 8
  • 13
1

you must customize your GridView

 GridView::PDF => [
    'filename' => 'Preceptors',
    'config' => [
      'methods' => [
        'SetHeader' => [
          ['odd' => $pdfHeader, 'even' => $pdfHeader]
        ],
        'SetFooter' => [
          ['odd' => $pdfFooter, 'even' => $pdfFooter]
        ],
      ],
      'options' => [
        'title' => 'Preceptors',
        'subject' => 'Preceptors',
        'keywords' => 'pdf, preceptors, export, other, keywords, here'
      ],
    ]
  ],

Where $pdfHeader and $pdfFooter

$pdfHeader = [
    'L'    => [
        'content' => 'LEFT CONTENT (HEAD)',
    ],
    'C'    => [
        'content'     => 'CENTER CONTENT (HEAD)',
        'font-size'   => 10,
        'font-style'  => 'B',
        'font-family' => 'arial',
        'color'       => '#333333',
    ],
    'R'    => [
        'content' => 'RIGHT CONTENT (HEAD)',
    ],
    'line' => true,
];

$pdfFooter = [
    'L'    => [
        'content'     => 'LEFT CONTENT (FOOTER)',
        'font-size'   => 10,
        'color'       => '#333333',
        'font-family' => 'arial',
    ],
    'C'    => [
        'content' => 'CENTER CONTENT (FOOTER)',
    ],
    'R'    => [
        'content'     => 'RIGHT CONTENT (FOOTER)',
        'font-size'   => 10,
        'color'       => '#333333',
        'font-family' => 'arial',
    ],
    'line' => true,
];

Look mPdf docs

Andrey Matveev
  • 145
  • 1
  • 7