Welcome Guest, Not a member yet? Register   Sign In
[Solved] Implement Mpdf
#1

(This post was last modified: 01-26-2020, 06:38 AM by PHS.)

Hello friends!

I'm trying to implement the mpdf library in CodeIgniter 4 like this:

Accessing App/Config/Constants.php includes the line of code:

Code:
defined('MPDF') || define('MPDF', ROOTPATH . 'vendor/autoload.php');


In the App/Config/Autoload.php file, I added one more element to the psr4 array:


Code:
$psr4 = [
'Config'       => APPPATH. 'Config',
APP_NAMESPACE => APPPATH,
'App'          => APPPATH,
'Mpdf'         => APPPATH. 'Libraries',
];

In the App/Libraries file, I created a class to instantiate Mpdf:


Code:
<?php namespace App\Libraries;

class Mpdf {

     public $method;

     function __construct() {

           require_once MPDF;

           $this->method = new \Mpdf\Mpdf (['mode' => 'utf-8']);
      }
}


In the Controller, I'm calling the Mpdf class like this:


Code:
use App\Libraries\Mpdf;

....

public function witeForm() {

     $mpdf = new Mpdf();
     $mpdf->method->WriteHTML('Hello  World');

      return $mpdf->method->Output();
}


But the displayed page is corrupted, as seen in the link below. 

Image corrupt

My question is: what is the correct way to implement Mpdf in CodeIgniter 4?

Note: I included Mpdf to the project via Codeigniter composer

Thanks in advance!
Reply
#2

(This post was last modified: 01-26-2020, 12:55 AM by jreklund.)

You don't need to do any of that. Composer are automatically loaded if you have it installed in; ROOTPATH.'vendor/autoload.php'.
So skip the whole Config and library thing and just use this in the controller. What you are doing above only needs to be done if you are loading an third party script without support for composer (or don't want it).

PHP Code:
use \Mpdf\Mpdf;

public function 
witeForm() {

     $mpdf = new Mpdf(['mode' => 'utf-8']);
     $mpdf->method->WriteHTML('Hello  World');

      return $mpdf->method->Output();


If you want to load a psr4 compatible library without Composer, you do it like this. (Don't know if it works for Mpdf).
PHP Code:
$psr4 = [
        'Config'      => APPPATH 'Config',
        APP_NAMESPACE => APPPATH,
        'App'         => APPPATH,
        'Mpdf'        => APPPATH .'ThirdParty/mpdf/src',
    ]; 
Reply
#3

(This post was last modified: 01-26-2020, 08:09 AM by PHS.)

(01-26-2020, 12:48 AM)jreklund Wrote: You don't need to do any of that. Composer are automatically loaded if you have it installed in; ROOTPATH.'vendor/autoload.php'.
So skip the whole Config and library thing and just use this in the controller. What you are doing above only needs to be done if you are loading an third party script without support for composer (or don't want it).

PHP Code:
use \Mpdf\Mpdf;

public function 
witeForm() {

     $mpdf = new Mpdf(['mode' => 'utf-8']);
     $mpdf->method->WriteHTML('Hello  World');

      return $mpdf->method->Output();


If you want to load a psr4 compatible library without Composer, you do it like this. (Don't know if it works for Mpdf).
PHP Code:
$psr4 = [
        'Config'      => APPPATH 'Config',
        APP_NAMESPACE => APPPATH,
        'App'         => APPPATH,
        'Mpdf'        => APPPATH .'ThirdParty/mpdf/src',
    ]; 

jreklund thank you for your help!

I followed what you guided and it worked!

In the controller the code for Mpdf looked like this:



PHP Code:
use \Mpdf\Mpdf;

public function 
witeForm() {

        $mpdf = new Mpdf(['mode' => 'utf-8']);
        $mpdf->WriteHTML('Hello World');
        return redirect()->to($mpdf->Output('filename.pdf''I'));
    
Reply
#4

Great! Thanks for letting us know. :-)
Reply




Theme © iAndrew 2016 - Forum software by © MyBB