How to use mpdf in yii2

Hi Again XD, I have another problem, this time it is about using mpdf to convert the content of my gridview into pdf, and well, I don’t know how to make it work for me.
I already have the extension installed, I did it by composer, but my problem is that in the examples of how to use it on its main page


mPDF - © Kartik

it only shows you the code for the controller but not for calling it from a view.

In order to help you you will need to explain the use-case you are trying to solve. Usually we use this extension to generate a pdf file of invoice or data table so you need to have a button on web-page of those and that button will submit to an action in your controller to create them.

If you already implemented that. please share detailed error so we can help more

Good Luck!

There is my problem, I am new to the yii2 framework, and I have no idea how to create a button that calls a controller, I only modify the code that the framework provides me, creating one from scratch I have no idea, in the main page there is a code which you have to use in your controller but the code to call it from a view is not on the page and I wouldn’t know how to do it myself.

This is the controller i want to call from a view, i want to see how this works and then use it to export gridview to a pdf

public function actionViewpdf($id) 
{
    Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
    $pdf = new Pdf([
        'mode' => Pdf::MODE_CORE, // leaner size using standard fonts
        'destination' => Pdf::DEST_BROWSER,
        'content' => $this->renderPartial('viewpdf'),
        'options' => [
            // any mpdf options you wish to set
        ],
        'methods' => [
            'SetTitle' => 'Privacy Policy - Krajee.com',
            'SetSubject' => 'Generating PDF files via yii2-mpdf extension has never been easy',
            'SetHeader' => ['Krajee Privacy Policy||Generated On: ' . date("r")],
            'SetFooter' => ['|Page {PAGENO}|'],
            'SetAuthor' => 'Kartik Visweswaran',
            'SetCreator' => 'Kartik Visweswaran',
            'SetKeywords' => 'Krajee, Yii2, Export, PDF, MPDF, Output, Privacy, Policy, yii2-mpdf',
        ]
    ]);
    return $pdf->render();
}

In default view “actionView” create a button like this…
<a class="btn btn-primary" href="[your-controller]/viewpdf/[id]">Export to PDF</a>

better change view (content of pdf view file name “$this->renderPartial(‘viewpdf’),”) to something else so you don’t get confused.

Call it… export-content → $this->renderPartial(‘export-content’), -Ofcourse, you have to create new file “export-content.php”-

This should work.