Export & download html to pdf in codeigniter 3 using mPDF erro:Class 'Mpdf\Mpdf' not found Export & download html to pdf in codeigniter 3 using mPDF erro:Class 'Mpdf\Mpdf' not found codeigniter codeigniter

Export & download html to pdf in codeigniter 3 using mPDF erro:Class 'Mpdf\Mpdf' not found


You are using

require_once __DIR__ . '/vendor/autoload.php';

inside your controller file. __DIR__ evaluates to the directory of the file that used it. Since this is a controller file, you are trying to load

/opt/lampp/htdocs/ticketing_tool_v2/application/controllers/admin/vendor/autoload.php

as __DIR__ evaluated to /opt/lampp/htdocs/ticketing_tool_v2/application/controllers/admin which is not the correct path.

A correct code would be

require_once __DIR__ . '/../../vendor/autoload.php';

A better solution is to use APPPATH so you wouldn't have to know the relative path.

require_once APPPATH.'vendor/autoload.php';

You can also

$config['composer_autoload'] = true; 

You can find more info here.

Lastly, in the GitHb readme.md you can see this.

It is recommended to set one's own temporary directory via tempDir configuration variable. The directory must have write permissions (mode 775 is recommended) for users using mPDF (typically cli, webserver, fpm).

This means that you need a writable folder for temporary operations. You can initialize the temp location to any place you prefer using

$mpdf = new \Mpdf\Mpdf(['tempDir' => '/tmp']);

/tmp is always writable. However, you can make any folder and path the path for it as a parameter like

$path = '/tmp/mpdf'; // You should change this as prefered.if (!file_exists($path)) {    mkdir($path, 0777, true);}$mpdf = new \Mpdf\Mpdf(['tempDir' => $path]);


Make sure your composer/manual mpdf installation is located in the directory

application\vendor

set in your config\config.php

$config['composer_autoload'] = true; 

or use in your controller:

require_once (APPPATH. 'vendor/autoload.php');

Depending on the version of mpdf installed, you can use namespaces (V.7+) like:

$mpdf = new \Mpdf\Mpdf();

Other versions (V.6.x) use:

$mpdf = new mPDF();

so just change your function like this:

function DownloadPdf(){    //require_once (APPPATH. 'vendor/autoload.php'); //-> you don't need this line if set in config.php    $mpdf = new mPDF();    $mpdf->WriteHTML('<h1>Hello world!</h1>');    $mpdf->Output();    }

issue documented


From the screenshot, the Issue doesn't seem to be the mPDF, it seems to be the data.Error 1: You have undefined incidents, this is causing the issue to create the PDF.

Error 2: You are passing an Object, however, mPDF requires you to pass an Array. You will need to convert the Model Return data using asArray or simply using return type of array for the model.

Reference: https://codeigniter4.github.io/userguide/models/model.html

Also, you don't need to explicitly define the autoload in your controller.Another thing to notice from the screenshot is it is creating the PDF, however, because the errors are throw, you can't see the PDF properly.

UPDATE: Potential Issue maybe that data is not passed to the View at all. Following code must be passing empty data

 $html = $this->load->view('admin/incidents/incidentsPdf',[],true);

Here [] is passing the empty data, so the View may not be receiving any data even though it is set in the controller.