Mpdf in codeigniter

Asked

Viewed 955 times

0

I’m using the library Mpdf in my project. I installed via Composer and locally with php version 7.1 (O php of the server in production is 7.2) and is enabled composer_autoload of CodeIgniter. Everything is working perfectly in the local environment, but when climbing the project in production, gives error saying that the "class mpdf not found":

Message: Class 'Mpdf Mpdf' not found

What may be happening? Why does the system locally find the class and in production?.

  • has already run dump-autoload on the production server?

  • @Rafaelmenabarreto not and honestly do not know how to do, I will search how to do or would have some tutorial link to send me? And it will not give problem with this command in other Ports that uses libraries installed via Poser?

  • You have installed mpdf by Composer?

  • @Rafaelmenabarreto yes, I installed locally via Composer and when I went up to production it presents that error

  • Edit your question and add information see here. Show how you are loading the class to the application. Tell the versions of the libraries you are using. What is the difference between the local environment and the production environment? OS?

1 answer

1

First, note that the configuration item $config['composer_autoload'] setado to TRUE will only upload libraries within application/vendor. That implies, at the very least, that there is a composer.json inside the application folder. If you installed the mPDF Somewhere else, she just won’t carry it. Second, a third party library must be declared in order to be instantiated in the application.

To start, state the location where you installed the mPDF. This is the most delicate part, because here you will have to indicate exactly where he saved the vendor of mPDF. The example below is in an environment Linux, but serves for any operating system. I added the following at the end of application/config/constants.php:

defined('MPDF') or define('MPDF', "/usr/share/php/mpdf/mpdf-7.1.9/vendor/autoload.php");

Once done, save the library statement in the file application/libraries/Mpdf.php:

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Mpdf {

    public $method;

    function __construct() {
        require_once MPDF;
        $this->method = new \Mpdf\Mpdf();
    }

}

Load the declared library with autoload in application/config/autoload.php:

$autoload['libraries'] = array(
    'mpdf'
); 

Example of use in a method of a controller:

function export() {
    $filename = 'sample.pdf';
    $html = '<html><body><p>Put some html here!</p></body></html>';
    $this->mpdf->method->SetTitle($filename);
    $this->mpdf->method->SetAuthor('Author name here');
    $this->mpdf->method->SetCreator('Creator name here');
    $this->mpdf->method->SetSubject('Testing mPDF create functionality on this system');
    $this->mpdf->method->WriteHTML($html);
    $this->mpdf->method->Output($filename, 'D');
}

There, everything in its place, can not go wrong. Doing so, no matter in which environment you publish the code, will work.

OBS: any library PHP can be loaded this way into a system made with CodeIgniter v3.1.10.

  • 1

    Congratulations on the well explained answer! It was not I who asked for help, but I had the same difficulties with mPdf and your answer was perfect to solve my problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.