Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kartik Export - How to style exported PDF files?

I am using Kartik Export (kartik\export\ExportMenu) to export data from gridview (kartik\grid\GridView) to PDF file. The problem is that the file has a small font and data seems jumbled up since there are no table borders or other formatting present. My question is - How can I add custom styles to the exported document?

This is the code I am using in my view file:

<?= GridView::widget([
    'dataProvider' => $provider,
    'columns' => $columns,
]); ?>

<?= ExportMenu::widget([
    'dataProvider' => $provider,
    'columns' => $export_columns,
    'target' => ExportMenu::TARGET_SELF,
    'showConfirmAlert' => false,
    'showColumnSelector' => false,
    'exportConfig' => [
        ExportMenu::FORMAT_HTML => false,
        ExportMenu::FORMAT_TEXT => false,
    ],
    'filename' => 'exported-data_' . date('Y-m-d_H-i-s'),
]); ?>
like image 401
eggnukes Avatar asked Feb 10 '23 04:02

eggnukes


2 Answers

In your exportConfig array add this setting:

GridView::PDF => [
   'config' => [
       'cssFile' => '@webroot/css/report.css',
   ]
]

Now you're able to create report.css in the css folder of you web folder and you can style ahead. This will replace the default bootstrap stylesheet, but reading your question it looks like it didn't get loaded anyways.

You'll find other configuration options for PDF here: [http://demos.krajee.com/grid#default-export-config]

like image 109
jonazu Avatar answered Feb 11 '23 18:02

jonazu


The difference between GridView and ExportMenu is that the ExportMenu uses PHPExcel library to generate the PDF via mPDF (or any other lib you need) while GridView directly offers method to use the mPDF library.

The formatting is controlled by PHPExcel settings (header, footer, fonts) - check the PHPExcel Documentation for details.

like image 38
Kartik V Avatar answered Feb 11 '23 17:02

Kartik V