Jason G
Jason G

Reputation: 2733

yii2 response formatter not being called

I'm trying to use robregonm pdfresponseFormatter to create a PDF, problem is, the pdf class is never called as the format reponse type.

    'response' => [
        'formatters' => [
            'html' => [
                'class' => 'yii\web\HtmlResponseFormatter',
            ],
            'pdf' => [
                'class' => 'robregonm\pdf\PdfResponseFormatter',
                'mode' => '',
                'format' => 'A4',
                'defaultFontSize' => 0,
                'defaultFont' => '',
                'marginLeft' => 15,
                'marginRight' => 15,
                'marginTop' => 16,
                'marginBottom' => 16,
                'marginHeader' => 9,
                'marginFooter' => 9,
            ]
        ]
    ],
....
$file = $this->getPdf($report);
....
private function getPdf($report){
    Yii::$app->response->format = 'pdf';

   Yii::$container->set(Yii::$app->response->formatters['pdf']['class']);

    $this->layout = '//attachment';

    return $this->render('pdf/actionplan', ['model' => $report]);
}

the $file variable has the HTML text in it from the view

Upvotes: 0

Views: 1273

Answers (2)

Jason G
Jason G

Reputation: 2733

Unfortunately I've not been able to get this extension to work. I did get kartik pdf to work great

I recommend using the kartik extension instead.

Upvotes: 0

Mihai P.
Mihai P.

Reputation: 9357

You are asking the wrong question. Of course $file has HTML in it, you are returning HTML from your view.

Yii::$app->response->format = 'pdf'; 

works when the controller returns a response to the browser.

What you want to ask is why the controller function (I hope this is in a controller) does not return a PDF created from the HTML.

If you just want to save a PDF look how I handle this: https://github.com/Mihai-P/yii2-core/blob/master/components/Controller.php in the actionPdf function. That function returns a file that you can save on the server if you want.

Upvotes: 0

Related Questions