Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter 4 - Using mPDF
#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


Messages In This Thread
CodeIgniter 4 - Using mPDF - by Luís Andrade - 10-12-2022, 09:20 AM
RE: CodeIgniter 4 - Using mPDF - by jasaekspedisi - 10-12-2022, 09:26 AM
RE: CodeIgniter 4 - Using mPDF - by abatrans - 10-12-2022, 01:19 PM
RE: CodeIgniter 4 - Using mPDF - by Luís Andrade - 10-16-2022, 07:01 AM



Theme © iAndrew 2016 - Forum software by © MyBB