Search code examples
phpyii2mpdfyii2-basic-app

Yii2 ~ How do you setup Mpdf?


after installing Mpdf via composer. and reading the documentation over and over agian, I still don't understand how to use Mpdf...

could someone please tell me how this extention is used? maybe with some examples or maybe you have a great video tutorial I could use? the problem is that I need to use this extention for an assignment. otherwise I wouldn't even use it =)

~~~~~~EDIT~~~~~~

I guess I wasn't clear enough... this is the problem I have:

it reders like this:

%PDF-1.4 %���� 3 0 obj <> /Contents 4 0 R>> endobj 4 0 obj <> stream x��SMO�@�ﯘ�F��}��n9b�D��fj��nK[�HL�m;;�޼�-VP��k6T�lW�Ɏഒ. �c��ͮ؞��V��0�GMl�-i&^^AU{�h�1\q$�6�� ��������᠇��5#]?���G�l�x�S��4�f1[.�~��D: �ѫ��GMdU�����y駿�b��Lʂ�6�mv�"&:TF��(���sW���X�~Z�Q�8�R'�F���{������1��J�*��)��ހR�h�\g�{s���ii��2��2����C��Y��U$��P��'�Nj���}^�v�*�=αzȻT_�GA����-+W�vq1�n��w�.(�dk�2{�7;�������.�� endstream endobj 1 0 obj <> endobj 5 0 obj <> endobj 6 0 obj <> endobj 7 0 obj <> endobj 8 0 obj <> endobj 9 0 obj <> endobj 2 0 obj <> /ExtGState << /GS1 5 0 R >> >> endobj 10 0 obj << /Producer (��mPDF 6.1) /Title (��Privacy Policy - Krajee.com) /Subject (��Generating PDF files via yii2-mpdf extension has never been easy) /CreationDate (20170912090628+02'00') /ModDate (20170912090628+02'00') >> endobj 11 0 obj << /Type /Catalog /Pages 1 0 R /OpenAction [3 0 R /XYZ null null 1] /PageLayout /OneColumn >> endobj xref 0 12 0000000000 65535 f 0000000684 00000 n 0000001237 00000 n 0000000015 00000 n 0000000223 00000 n 0000000773 00000 n 0000000834 00000 n 0000000932 00000 n 0000001028 00000 n 0000001129 00000 n 0000001383 00000 n 0000001715 00000 n trailer << /Size 12 /Root 11 0 R /Info 10 0 R /ID [<07b9b14c395d9d474f3b80fce884ce97> <07b9b14c395d9d474f3b80fce884ce97>] >> startxref 1825 %%EOF


Solution

  • If you haven't installed mPDF for Yii2 by Kartik-v, please install that one instead your current version. Here's the link: http://demos.krajee.com/mpdf#installation

    They also wrote some great documentation and examples. Please follow them at their website: http://demos.krajee.com/mpdf#demo

    In your controller:

    //hint: add this after the controller namespace. (top of the file)
    use kartik\mpdf\Pdf;
    
    // Privacy statement output demo
    public function actionMpdfDemo1() {
        $pdf = new Pdf([
            'mode' => Pdf::MODE_CORE, // leaner size using standard fonts
            'content' => $this->renderPartial('privacy'),
            'options' => [
                'title' => 'Privacy Policy - Krajee.com',
                'subject' => 'Generating PDF files via yii2-mpdf extension has never been easy'
            ],
            'methods' => [
                'SetHeader' => ['Generated By: Krajee Pdf Component||Generated On: ' . date("r")],
                'SetFooter' => ['|Page {PAGENO}|'],
            ]
        ]);
        return $pdf->render();
    }
    

    In any view file:

    /**
     * THE VIEW BUTTON
     */
    echo Html::a('<i class="fa glyphicon glyphicon-hand-up"></i> Privacy Statement', ['/site/mpdf-demo-1'], [
        'class'=>'btn btn-danger', 
        'target'=>'_blank', 
        'data-toggle'=>'tooltip', 
        'title'=>'Will open the generated PDF file in a new window'
    ]);
    

    EDIT

    You must create view file named privacy.php. and add it on the same folder as your previous view folder. write some html content on it, it shouldn't return crap.