Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Uncaught Error: Class Mpdf\Mpdf not found

ErrorFatal error: Uncaught Error: Class "Mpdf\Mpdf" not found

Short answer

PHP cannot find the class because mPDF is not installed, the Composer autoloader is not loaded, or the name is resolved inside another namespace. Run composer require mpdf/mpdf, add require_once __DIR__ . '/vendor/autoload.php' at the top, and write new \Mpdf\Mpdf().

  • Applies to PHP 8.3, mPDF 8, Composer 2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check that the package is in vendor and that the autoloader file exists next to your script's project.

    composer show mpdf/mpdf
    ls vendor/autoload.php
  2. 2Match what you see, then jump to its fix

    1. 1composer show says the package is not found, or there is no vendor foldermPDF is not installed with ComposerFix
    2. 2The package is installed but the error remainsvendor/autoload.php is not required, or the path is wrongFix
    3. 3The error names the class with a longer prefix, or the file starts with a namespace lineThe class name is used inside a namespace without a leading backslashFix
    4. 4Works locally, fails on the servervendor was not uploaded to the serverFix

What does the error look like?

  • Fatal error: Uncaught Error: Class "Mpdf\Mpdf" not found

Cause 1 of 4

mPDF is not installed with Composer

1Check

The official installation method for mPDF is Composer and the package mpdf/mpdf. A copy of the library dropped into a folder by hand has no autoloader.

composer show mpdf/mpdf

2Fix

Install it from the project folder that holds composer.json.

composer require mpdf/mpdf

3Verify

composer show prints the package and vendor/mpdf exists.

composer show mpdf/mpdf

Confirmed by mpdf.github.io

Did this fix it?

Cause 2 of 4

vendor/autoload.php is not required, or the path is wrong

1Check

Composer writes vendor/autoload.php, and including it makes the installed classes usable. A script in a subfolder with a relative path that points to the wrong directory misses it.

grep -n "autoload" pdf.php

2Fix

Require the autoloader once, at the top, using __DIR__ to build a path that cannot move.

<?php
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();

3Verify

The script runs past the new \Mpdf\Mpdf() line.

php pdf.php

Confirmed by getcomposer.org

Did this fix it?

Cause 3 of 4

The class name is used inside a namespace without a leading backslash

1Check

mPDF 7 and later use namespaces. The docs create the object with the full name \Mpdf\Mpdf, or import the class with a use statement.

head -n 5 pdf.php

2Fix

Add a leading backslash, or a use statement at the top.

use Mpdf\Mpdf;
$mpdf = new Mpdf();

3Verify

The class is found and WriteHTML() runs.

$mpdf->WriteHTML('<h1>Hello world!</h1>');
$mpdf->Output();

Confirmed by mpdf.github.io

Did this fix it?

Cause 4 of 4

vendor was not uploaded to the server

1Check

Composer downloads dependencies into the vendor directory, and a lock file records the versions. If vendor was not deployed, the server has no library.

ls vendor | head

2Fix

Run composer install on the server, so that it reads composer.lock and installs the same versions.

composer install

3Verify

vendor/autoload.php exists on the server and the page produces a PDF.

ls vendor/autoload.php

Confirmed by getcomposer.org

Did this fix it?

How do I stop it coming back?

  • Add vendor/autoload.php once in a shared bootstrap file.
  • Deploy with composer install, not by copying folders.
  • Set a writable tempDir for mPDF so it does not fail later on file creation.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.