Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter 4 - Using mPDF
#1

(This post was last modified: 10-12-2022, 10:15 AM by Luís Andrade.)

Hello, Guys!
I've been migrating CI3 systems to CI4 for some time.
It's not too insane the task because CI4 makes the code cleaner and shorter and with a very short learning curve, for those who already know CI3.
Now we will quickly talk about how to use the mPDF library in CI4
Installing using composer is the best path to success.
Install composer on your computer and go to your system folder.

https://getcomposer.org/download/

See instructions:
https://mpdf.github.io/installation-setu...-v7-x.html

Run:
$ composer require mpdf/mpdf

It may ask if you want it to use the composer.json file from CI4. Answer Y
Done! You don't need anything else! Just code!

mPDF < v8.x asks for ext-gd extension. So... it's easy to enable it.
//******************
From https://stackoverflow.com/questions/2283...without-gd
answered Aug 22, 2021 at 21:53
Mohamed Hany
PHP7 Or PHP8 For Windows:

Check if in your php.ini file has the following line:

;extension=gd
if exists, change it to

extension=gd
if not found, Add this

extension=gd
and restart apache

(it works on WINDOWS)
//******************

In your code for PDF it will like this:
$this->mpdf is my choice, and you can use any name you want, p.ex., $this->mympdf, $this->mydog, $this->otherpdf... 

//Basic config
$this->mpdf = new \Mpdf\Mpdf([
            'mode' => 'c',
            'format' => 'A4-L',
            'default_font_size' => '12',
            'default_font' => 'Helvetica',
            'margin_left' => 20,
            'margin_top' => 20,
            'margin_right' => 20,
            'margin_bottom' => 20,
        ]);

//For header and footer you need adapt for your needs. This is my easy example.

//**** PDF Header
        $header = array(
            'L' => array(
                'content' => $myContentTitle,
                'font-size' => 9,
                'font-style' => 'B',
                'font-family' => 'sans-serif',
                'color' => '#000000'
            ),
            'C' => array(
                'content' => $myContentTitle,
                'font-size' => 8,
                'font-style' => 'B',
                'font-family' => 'sans-serif',
                'color' => '#000000'
            ),
            'line' => 1, //line 1 = ON
            'R' => array(//RIGHT
                'content' => utf8_encode($someText),
                'font-size' => 8,
                'font-style' => 'B',
                'font-family' => 'sans-serif',
                'color' => '#000000'
            ),
        );

        //**** PDF footer
        $footer = array(
            'L' => array(
                'content' => '{DATE j/m/Y}- ',
                'font-size' => 9,
                'font-style' => 'B',
                'font-family' => 'sans-serif',
                'color' => '#000000'
            ),
            'C' => array(
                'content' => '{PAGENO}/{nbpg}',
                'font-size' => 9,
                'font-style' => 'B',
                'font-family' => 'sans-serif',
                'color' => '#000000'
            ),
            'line' => 1, //line 1 = ON
            'R' => array(//RIGHT
                'content' => '    ',
                'font-size' => 9,
                'font-style' => 'B',
                'font-family' => 'sans-serif',
                'color' => '#000000'
            )
        );
        //***************

$data= "your data here";
$html = view('rep/rep_to_pdf', $data);
$this->mpdf->SetHeader($header, 'O');
$this->mpdf->SetFooter($footer, 'O');

//Here you can use Bootstrap CSS from
//https://stackoverflow.com/questions/49102418/how-to-use-bootstrap-in-mpdf
//https://github.com/kartik-v/yii2-mpdf/blob/master/src/assets/kv-mpdf-bootstrap.css

$this->mpdf->WriteHTML(path to bootstrap CSS, 1);
$this->mpdf->SetDisplayMode('fullpage');
$this->mpdf->WriteHTML($html, 2);      
$this->mpdf->Output($nome_arquivo, 'D'); //option "D" save file

Now you have your PDF Smile

Configuration of mPDF:
https://mpdf.github.io/

Do your best, always!
Reply
#2

wow nice
Reply
#3

Glad to see I am not the only one recommending mPDF. 

I have written a helper function to also add custom fonts when you create the mPDF object.


PHP Code:
/**
* @param string $orientation Values 'P' or 'L'
* @param int    $mt Page top margin in mm
* @param int    $mr Page right margin in mm
* @param int    $mb Page bottom margin in mm
* @param int    $ml Page left margin in mm
* @param int    $mh Header margin in mm
* @param int    $mf Footer margin in mm
*
* @return \Mpdf\Mpdf
* @throws \Mpdf\MpdfException
*/
function myPdfstring $orientation 'P'int $mt 40int $mr 10int $mb 20int $ml 10int $mh 8int $mf ): \Mpdf\Mpdf {
    $defaultFontConfig = ( new Mpdf\Config\FontVariables() )->getDefaults();
    $fontData          $defaultFontConfig'fontdata' ] + [
            'homemadeapple'  => [
                'R' => 'HomemadeApple-Regular.ttf',
            ],
            'jennasue'        => [
                'R' => 'JennaSue.ttf',
            ],
            'kaufmann'        => [
                'R' => 'Kaufmann BT.ttf',
            ],
            'roboto'          => [
                'R'  => 'Roboto-Regular.ttf',
                'I'  => 'Roboto-Italic.ttf',
                'B'  => 'Roboto-Bold.ttf',
                'BI' => 'Roboto-BoldItalic.ttf',
            ],
            'robotocondensed' => [
                'R'  => 'RobotoCondensed-Regular.ttf',
                'I'  => 'RobotoCondensed-Italic.ttf',
                'B'  => 'RobotoCondensed-Bold.ttf',
                'BI' => 'RobotoCondensed-BoldItalic.ttf',
            ],
        ];
    $defaultConfig    = ( new Mpdf\Config\ConfigVariables() )->getDefaults();
    $fontDirs          $defaultConfig'fontDir' ];
    if ( $orientation === 'L' )
    {
        $return = new Mpdf\Mpdf(
            [
                'format'        => 'A4-L',
                'margin_left'  => $ml,
                'margin_right'  => $mr,
                'margin_top'    => $mt,
                'margin_bottom' => $mb,
                'margin_header' => $mh,
                'margin_footer' => $mf,
                'orientation'  => 'L',
                'mode'          => 's',
                'fontDir'      => array_merge$fontDirs, [FCPATH 'public/assets-app/fonts',] ),  // path to the font files
                'fontdata'      => $fontData,
            ] );
    }
    else
    {
        $return = new Mpdf\Mpdf(
            [
                'format'        => 'A4-P',
                'margin_left'  => $ml,
                'margin_right'  => $mr,
                'margin_top'    => $mt,
                'margin_bottom' => $mb,
                'margin_header' => $mh,
                'margin_footer' => $mf,
                'orientation'  => 'P',
                'mode'          => 's',
                'fontDir'      => array_merge$fontDirs, [FCPATH 'public/assets-app/fonts',] ), // path to the font files
                'fontdata'      => $fontData,
            ] );
    }
    return $return;

Dirk B.
Abatrans Software
No SEO spam - see forum guidelines
Reply
#4

(This post was last modified: 10-16-2022, 07:05 AM by Luís Andrade.)

(10-12-2022, 01:19 PM)abatrans Wrote: Glad to see I am not the only one recommending mPDF. 

I have written a helper function to also add custom fonts when you create the mPDF object.


PHP Code:
/**
* @param string $orientation Values 'P' or 'L'
* @param int    $mt Page top margin in mm
* @param int    $mr Page right margin in mm
* @param int    $mb Page bottom margin in mm
* @param int    $ml Page left margin in mm
* @param int    $mh Header margin in mm
* @param int    $mf Footer margin in mm
*
* @return \Mpdf\Mpdf
* @throws \Mpdf\MpdfException
*/
function myPdfstring $orientation 'P'int $mt 40int $mr 10int $mb 20int $ml 10int $mh 8int $mf ): \Mpdf\Mpdf {
    $defaultFontConfig = ( new Mpdf\Config\FontVariables() )->getDefaults();
    $fontData          $defaultFontConfig'fontdata' ] + [
            'homemadeapple'  => [
                'R' => 'HomemadeApple-Regular.ttf',
            ],
            'jennasue'        => [
                'R' => 'JennaSue.ttf',
            ],
            'kaufmann'        => [
                'R' => 'Kaufmann BT.ttf',
            ],
            'roboto'          => [
                'R'  => 'Roboto-Regular.ttf',
                'I'  => 'Roboto-Italic.ttf',
                'B'  => 'Roboto-Bold.ttf',
                'BI' => 'Roboto-BoldItalic.ttf',
            ],
            'robotocondensed' => [
                'R'  => 'RobotoCondensed-Regular.ttf',
                'I'  => 'RobotoCondensed-Italic.ttf',
                'B'  => 'RobotoCondensed-Bold.ttf',
                'BI' => 'RobotoCondensed-BoldItalic.ttf',
            ],
        ];
    $defaultConfig    = ( new Mpdf\Config\ConfigVariables() )->getDefaults();
    $fontDirs          $defaultConfig'fontDir' ];
    if ( $orientation === 'L' )
    {
        $return = new Mpdf\Mpdf(
            [
                'format'        => 'A4-L',
                'margin_left'  => $ml,
                'margin_right'  => $mr,
                'margin_top'    => $mt,
                'margin_bottom' => $mb,
                'margin_header' => $mh,
                'margin_footer' => $mf,
                'orientation'  => 'L',
                'mode'          => 's',
                'fontDir'      => array_merge$fontDirs, [FCPATH 'public/assets-app/fonts',] ),  // path to the font files
                'fontdata'      => $fontData,
            ] );
    }
    else
    {
        $return = new Mpdf\Mpdf(
            [
                'format'        => 'A4-P',
                'margin_left'  => $ml,
                'margin_right'  => $mr,
                'margin_top'    => $mt,
                'margin_bottom' => $mb,
                'margin_header' => $mh,
                'margin_footer' => $mf,
                'orientation'  => 'P',
                'mode'          => 's',
                'fontDir'      => array_merge$fontDirs, [FCPATH 'public/assets-app/fonts',] ), // path to the font files
                'fontdata'      => $fontData,
            ] );
    }
    return $return;

Congratulations! Smile

(10-12-2022, 09:26 AM)jasaekspedisi Wrote: wow nice

Also about mPDF, see albatrans solution Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB