kartik GridView Yii2 - 如何配置PDF的导出配置(页眉,页脚,标题)

时间:2015-07-02 23:57:59

标签: pdf gridview yii2 yii-components kartik-v

我正在尝试找出一种在导出时修改PDF标题的方法。

现在标题说Yii2 Grid Export(PDF)Grid Export。

以下是我尝试修改它的代码:

            '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'
                    ]
                ],
            ],

2 个答案:

答案 0 :(得分:2)

只需扩展这个精美的kartik网格(http://demos.krajee.com/grid)并覆盖run()和initExport(),例如:

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();        
    }

}

当然可以根据需要设置命名空间。

答案 1 :(得分:1)

您必须自定义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'
      ],
    ]
  ],

其中$ pdfHeader和$ 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,
];

查看mPdf文档

相关问题