Error when instantiating object inside a namespace

Asked

Viewed 305 times

1

I’m trying to use the mpdf library to print in PDF. However, the following error is occurring when instantiating the library:

Fatal error: Trait 'Mpdf\Strict' not found in C:\wamp64\www\jlrw\PHP\transporte\app\lib\mPdf\src\Mpdf.php on line 44

I am not using framework. The mpdf library is in the folder: C:\wamp64\www\jlrw\PHP\transporte\app\lib

Follow the code I’m using in PHP:

session_start();
header('Content-Type: text/html; charset=utf-8');
$html = $_POST['dados'];

$sRoot = '..' . DIRECTORY_SEPARATOR;

require_once($sRoot . 'php' . DIRECTORY_SEPARATOR . 'definicoes.php');

// Aqui eu trago o arquivo principal da biblioteca
require_once($sRoot . 'lib' . DIRECTORY_SEPARATOR . 'mPdf' .
                              DIRECTORY_SEPARATOR . 'src' .
                              DIRECTORY_SEPARATOR . 'Mpdf.php');

// "Autoload"
function AutoLoad($classname) {
   $filename = '..' .
               DIRECTORY_SEPARATOR . 'lib' .
               DIRECTORY_SEPARATOR . 'mPdf' .
               DIRECTORY_SEPARATOR . 'src' .
               DIRECTORY_SEPARATOR . $classname . '.php';
   include_once($filename);
}

spl_autoload_register('AutoLoad');

$mpdf = new Mpdf();
$mpdf->WriteHTML($html);
$mpdf->Output();

echo 'Impressão realizada.';

At no time am I using namespace in my app. The version of my PHP is 7.2.4

The "Strict.php" file is in the "src" folder of the library. I don’t think it’s her mistake, but the way I’m trying to instate it for use.

  • 1

    I believe that the mpdf depends on the Composer (or equivalent) to load the internal classes. Neither Composer is using?

  • I didn’t get to use Poser either... I downloaded the library and put it in the folder. Is that the problem?

  • 1

    Yeah, see the source code from the library. In no time it imports the dependency files, leaving this responsibility to the tool of autoload who prefer.

  • I installed mpdf via Composer inside my application and directly in the "lib/mpdf" folder, but now it gives another error when instantiating the object: "Uncaught Error: Class 'Mpdf' not found"... it’s strange...

  • 1

    You imported Composer autoload?

  • Yes. I used the "require_once" command pointing directly to "autoload.php" inside the "vendor" folder that Composer created. I’m doing some more tests here. For now, thank you so much for your help.

Show 1 more comment
No answers

Browser other questions tagged

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