eggnukes
eggnukes

Reputation: 180

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'),
]); ?>

Upvotes: 4

Views: 6151

Answers (2)

Kartik V
Kartik V

Reputation: 457

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.

Upvotes: 3

jonazu
jonazu

Reputation: 380

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]

Upvotes: 5

Related Questions