0

我想在导出的文件中添加标题。我正在使用kartik Export in Yii2. 我期望的结果是这样的:

在此处输入图像描述

这是我的观点: https ://pastebin.com/HDeuQnfj

谁能帮我找到解决方案?这对我有很大帮助。提前致谢

4

1 回答 1

2

这不是超级容易。如果您只想在 PDF 的标题中添加文本,请尝试:

echo ExportMenu::widget([
    'dataProvider' => $dataProvider,
    'columns' => $gridColumns,
    'filename' => 'Exported File',
    'fontAwesome' => true,
    'exportConfig' => [
        ExportMenu::FORMAT_PDF => [
            'pdfConfig' => [
                'methods' => [
                    'SetHeader' => ['Your Title Here'], 
                ],
            ],
        ],
    ],
]);

但是如果你想在 PDF 的正文中使用它,我想你必须像这样扩展kartik\export\ExportMenu和覆盖renderPDF()

class MyExportMenu extends \kartik\export\ExportMenu
{
    protected function renderPDF($file)
    {
        //  Default PDF paper size
        $excel = $this->_objPHPExcel;
        $sheet = $this->_objPHPExcelSheet;
        /**
         * @var \PHPExcel_Writer_HTML $w
         */
        $w = $this->_objPHPExcelWriter;
        $page = $sheet->getPageSetup();
        $orientation = $page->getOrientation() == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE ? 'L' : 'P';
        $properties = $excel->getProperties();
        $settings = ArrayHelper::getValue($this->exportConfig, $this->_exportType, []);
        $useInlineCss = ArrayHelper::getValue($settings, 'useInlineCss', false);
        $config = ArrayHelper::getValue($settings, 'pdfConfig', []);
        $w->setUseInlineCss($useInlineCss);
        $config = array_replace_recursive(
            [
                'orientation' => strtoupper($orientation),
                'methods' => [
                    'SetTitle' => $properties->getTitle(),
                    'SetAuthor' => $properties->getCreator(),
                    'SetCreator' => $properties->getCreator(),
                    'SetSubject' => $properties->getSubject(),
                    'SetKeywords' => $properties->getKeywords(),
                ],
                'cssFile' => '',
                'content' => '<h1>Your Title Here</h1>' // <- title here
                    . $w->generateHTMLHeader(false) 
                    . $w->generateSheetData() 
                    . $w->generateHTMLFooter(),
            ],
            $config
        );
        if (!$this->stream) {
            $config['destination'] = Pdf::DEST_FILE;
            $config['filename'] = $file;
        } else {
            $config['destination'] = Pdf::DEST_DOWNLOAD;
            $extension = ArrayHelper::getValue($settings, 'extension', 'pdf');
            $config['filename'] = $this->filename . '.' . $extension;
        }
        $pdf = new Pdf($config);
        echo $pdf->render();
    }
}

然后你必须改用你的类。我不是 100% 确定它有效,未经测试。

于 2017-07-27T04:51:31.073 回答