如何在Yii2中使用export kartik下载的文档中添加标题? [英] How to add title in document that we download using export kartik in Yii2?

查看:347
本文介绍了如何在Yii2中使用export kartik下载的文档中添加标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





这是我的看法:
https://pastebin.com/HDeuQnfj



有人可以帮我找到解决方案吗?这将帮助我很多。感谢提前

解决方案

这不是很容易。如果您只想在PDF的标题中的文本尝试:

  echo ExportMenu :: widget([
'dataProvider'= $ dataProvider,
'columns'=> $ gridColumns,
'filename'=>'导出文件',
'fontAwesome'=> true,
' exportConfig'=> [
ExportMenu :: FORMAT_PDF => [
'pdfConfig'=> [
'methods'=> [
'SetHeader'=> ['你的头衔],
],
],
],
],
]);

但是如果你想要在PDF的正文中,我想你必须扩展 kartik\export\ExportMenu 并覆盖 renderPDF()如下:

  class MyExportMenu extends \kartik\export\ExportMenu 
{
protected function renderPDF($ file)
{
//默认PDF纸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>您的标题在这里< / 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。 ''。 $扩展;
}
$ pdf = new Pdf($ config);
echo $ pdf-> render();
}
}

然后你必须使用你的类。我不是100%确定它的工作,没有测试。


I want to add title in my exported file. I'm using kartik Export in Yii2. The result that I expected is like this:

Here is my view: https://pastebin.com/HDeuQnfj

Could anyone please help me to find the solution? It will help me a lot. Thanks in advance

解决方案

It's not super easy. If you just want your text in PDF's header try:

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

But if you want it in the PDF's body I guess you have to extend kartik\export\ExportMenu and override renderPDF() like:

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

And then you have to use your class instead. I'm not 100% sure it works, not tested.

这篇关于如何在Yii2中使用export kartik下载的文档中添加标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆