Yii2-Mpdf


(Kvisweswaran) #1

This is the forum for the yii2-mpdf extension for Yii framework 2.0. This extension is a great PDF component for Yii2 based on the mPDF library which can convert UTF-8 encoded HTML to PDF with various styling options

Refer documentation and usage.


(Keeran Nale) #2

include(mpdf.php): failed to open stream: No such file or directory


(Jcaceres) #3

Hi Kartiv,

We are using the extension to generate a pdf after the app generate a quote.

but we got rare symbols instead of the proper PDF file.

The config file:

      'components' => [


      // setup Krajee Pdf component


      'pdf' => [


           'class' => Pdf::classname(),


           'format' => Pdf::FORMAT_A4,


           'orientation' => Pdf::ORIENT_PORTRAIT,


           'destination' => Pdf::DEST_BROWSER,


           // refer settings section for all configuration options


      ],

the controller to call the report:

$pdf = Yii::$app->pdf;

$pdf->mode = Pdf::MODE_UTF8;

$pdf->content = ‘Hello Kartik from advanced action’;

return $pdf->render();

Do you have any idea what is going on??


(Brice) #4

I use this extension which works fine.

I have just a problem with checkbox who doesn’t appear in the pdf. This checkbox is just to display, operator will check it manually.

My conf:




...

        // setup kartik\mpdf\Pdf component

        $pdf = new Pdf([

            // set to use core fonts only

            'mode' => Pdf::MODE_CORE, 

            // A4 paper format

            'format' => Pdf::FORMAT_A3, 

            // portrait orientation

//            'orientation' => Pdf::ORIENT_PORTRAIT, 

            'orientation' => Pdf::ORIENT_LANDSCAPE, 

            // stream to browser inline

            'destination' => Pdf::DEST_BROWSER, 

            // your html content input

            'content' => $content,

            // format content from your own css file if needed or use the

            // enhanced bootstrap css built by Krajee for mPDF formatting 

            'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',

            // any css to be embedded if required

//            'cssInline' => '.kv-heading-1{font-size:18px}', 

            'cssInline' => '.tableavecborder{border:1px solid black},

                            .tableavecborder td{height:35px;min-height:35px},

                            .tdcenter{text-align:center}',

             // set mPDF properties on the fly

            'options' => ['title' => 'Dossier de travail ' . $model->document_id],

             // call mPDF methods on the fly

            'methods' => [ 

                'SetHeader'=>[''], 

//                'SetFooter'=>['{PAGENO} / {nbpg}'],

                'SetFooter'=>[''],

            ]

        ]);

...



and the part of the code (inside table)




...

	<input type="checkbox" name="origA4" value="1" checked="" /> A4

...



Thanks for your help.


(Ruffdee) #5

thanks for sharing


(Admin) #6

After several days studing the docs and trying to resolve the error that shows a lot of unreadable text in the same page like:


%PDF-1.4%����3 0 obj<> /Contents 4 0 R>>endobj4 0 obj<>streamx��S�n�@�������y{�$ !�K] %I�Vy���\�����q��Ti�,�=�uΜ;�׬�R��nfB���K/MT����{��<���e�JzU�����}�@�F�g[�}(�^ n#>aœƗ�8�9eWf�m|U�bN� ��(�9��W�tNS�]�kb2�HS�W�:�ƪ��Tb��%Ww�a�f]s��Hopj�d�ܶ�^��F�)���7|�KFemR��}@�;��� Q�ۮ�L�8


and finishing with:


exception ‘yii\web\HeadersAlreadySentException’ with message ‘Headers already sent in […]vendor\mpdf\mpdf\src\Mpdf.php on line 9356.’ in […]\vendor\yiisoft\yii2\web\Response.php:366


I have tested different solutions found in several posts with the same problem, like:

1- exit after the render,

2- if (headers_sent()) {return;},

3- ‘utf-8’,

4- Add pdf headers before outputing the pdf: Yii::$app->response->format = \yii\web\Response::FORMAT_RAW; Yii::$app->response->headers->add(‘Content-Type’, ‘application/pdf’);

and none of them works for me (YII2.0.16, PHP5.6.30)…

But the good part is that if I reload the same page in the browser (F5) the pdf is generated properly…

I have installed the robregonm/yii2-pdf to check and I have the same problem…

Any other solutions please?. How to fix HeadersAlreadySentException?. Please, I need a working pdf extension for yii2…

Thanks a lot, and Kartik congratulations for your great extensions!


(Apsprint) #7

I always make sure to use an empty layout, with no html at all when I render these.




<?php echo $content; ?>




(Admin) #8

Thank you newbiedo for your answer!

But I have tested this way with an empty layout and this not revolve me the problem…

Several hours later, I found finally the SOLUTION: It was that I placed the link to actionPdf (the action where I use the pdf class) inside the Pjax::begin() and Pjax::end() zone, so:

  1. In the controller/actionPdf (pdf as component in my case)

use kartik\mpdf\Pdf;

// This line first 

Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;  

// And continue, as component:

$pdf = Yii::$app->pdf;

$pdf->content = $this->renderPartial('_pdf', ['model' => $model]);

$pdf->filename = 'filename.pdf';

return $pdf->render();

  1. In the common/config/main.php:

use kartik\mpdf\Pdf;

'components' => [  

  // N. setup Krajee Pdf component with default values

  'pdf' => [

    'class' => Pdf::classname(),

    'format' => Pdf::FORMAT_A4,

    'orientation' => Pdf::ORIENT_PORTRAIT,

   ...

  1. In the view:

Html::a('PDF list', ['pdf']...

And the most important OUTSIDE of the Pjax::begin() and Pjax::end() zone.

This works for me!!!.

Perhaps helps someboby…


(Apsprint) #9

Glad you got it going.