Uncaught Error: Class Mpdf\Mpdf not found
Fatal error: Uncaught Error: Class "Mpdf\Mpdf" not foundShort 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().
Fix it fast
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.php2Match what you see, then jump to its fix
- 1
composer show says the package is not found, or there is no vendor foldermPDF is not installed with ComposerFix - 2
The package is installed but the error remainsvendor/autoload.php is not required, or the path is wrongFix - 3
The 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
Works locally, fails on the servervendor was not uploaded to the serverFix
- 1
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/mpdf2Fix
Install it from the project folder that holds composer.json.
composer require mpdf/mpdf3Verify
composer show prints the package and vendor/mpdf exists.
composer show mpdf/mpdfConfirmed by mpdf.github.io
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.php2Fix
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.phpConfirmed by getcomposer.org
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.php2Fix
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
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 | head2Fix
Run composer install on the server, so that it reads composer.lock and installs the same versions.
composer install3Verify
vendor/autoload.php exists on the server and the page produces a PDF.
ls vendor/autoload.phpConfirmed by getcomposer.org
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
require(vendor/autoload.php): Failed to open stream: No such file or directory
Fatal error: Uncaught Error: Failed opening required 'vendor/autoload.php'
PHP looked for vendor/autoload.php at a path where it does not exist.
3 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix