Skip to content

hanishsingla/mpdf-wrapper

Repository files navigation

mpdf-wrapper

This bundle provides a service for mpdf library with symfony 4.

1 Installation

1.1 Download MpdfWrapperBundle using composer

Run in terminal:

$ php composer.phar require symfgenus/mpdf-wrapper

1.2 Enable the bundle

Symfony flex activates the bundles automatically. If it is not, then enable the bundle:

// /config/bundles.php

    [
        // ...
        Symfgenus\MpdfWrapper\MpdfWrapperBundle::class => ['all' => true],
    ];

2 Usage

MpdfService provides many ways to use MPDF.

2.1 It can generate a direct pdf response which can be served through any route.

// /config/bundles.php

    public function index(MpdfService $MpdfService)
    {
        return $MpdfService->generatePdfResponse($pdfHtml);
    }

2.2 It can also generate pdf content which can be saved in a variable and used.

// /config/bundles.php

    public function index(MpdfService $MpdfService)
    {
        return $pdf = $MpdfService->generatePdf($pdfHtml);
    }

2.3 Sometimes there is need to create multiple PDFs, MpdfService can be used as following:

// /config/bundles.php

    public function index(MpdfService $MpdfService)
    {
        $firstPdf = $MpdfService->getMpdf($argsFirst);
        $mpdf->WriteHTML($htmlFirst);
        $firstPdfFile = $mpdf->Output();

        $secondPdf = $MpdfService->getMpdf($argsSecond);
        $mpdf->WriteHTML($htmlSecond);
        $secondPdfFile = $mpdf->Output();

        return [
            $firstPdfFile,
            $secondPdfFile
        ];
    }

3 Usage for symfony 3

For symfony 3, this service can be loaded as following:

$this->get('symfgenus.mpdf.wrapper').