Welcome Guest, Not a member yet? Register   Sign In
Can't load pdf with using Mpdf in Codeigniter 4
#8

I have been using Mpdf in my CI 3 projects and tested it in CI 4.  The method I used was as below:

I installed Mpdf via composer.

Then I created my own helper function to create the Mpdf object.  I needed to add custom fonts hence the additional code prior to obtaining the Mpdf object.

The Helper function definition
PHP Code:
function myPdf$orientation ) {

   $defaultFontConfig = ( new Mpdf\Config\FontVariables() )->getDefaults();
   $fontData          $defaultFontConfig'fontdata' ] + [
         'homemadeapple'        => [
            'R' => 'HomemadeApple-Regular.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' ) {
      $myPdf = new \Mpdf\Mpdf(
         array(
            'format'   => 'A4-L'
            'margin_left' => 10
            'margin_right' => 10
            'margin_top' => 30
            'margin_bottom' => 20
            'margin_header' => 8
            'margin_footer' => 8
            'orientation' => 'L'
            'mode' => 's',
            'fontDir'  => array_merge$fontDirs, [ FCPATH 'public/assets-app/fonts', ] ),
            'fontdata' => $fontData,
         ) );
   }
   else {
      $myPdf = new \Mpdf\Mpdf( array(
         'format'        => 'A4-P',
         'margin_left'   => 20,
         'margin_right'  => 10,
         'margin_top'    => 40,
         'margin_bottom' => 20,
         'margin_header' => 8,
         'margin_footer' => 8,
         'orientation'   => 'P',
         'mode'          => 's',
         'fontDir'       => array_merge$fontDirs, [ FCPATH 'public/assets-app/fonts', ] ),
         'fontdata'      => $fontData,
      ) );
   }

   return $myPdf;



Usage in a controller:
PHP Code:
   public function testPdf() {
      $pdf myPdf('P');

      $pdf->writeHTML"Hello World" );

      $pdf->OutputFCPATH "writable/uploads/test.pdf"'F');

      return redirect()->tosite_url('/') );

   
In my opinion mPdf works extremely well when taking an HTML block and sending it to writeHTML.  I found it much simpler and easier to use than for example tcpdf.

Regards
Dirk B.
Dirk B.
ESD Digital
No SEO spam - see forum guidelines
Reply


Messages In This Thread
RE: Can't load pdf with using Mpdf in Codeigniter 4 - by abatrans - 07-16-2020, 04:53 AM



Theme © iAndrew 2016 - Forum software by © MyBB