PHP Fatal error: Uncaught Error: Class "Mpdf\Mpdf" not found in C:\xampp\htdocs\server\pdf.php:5

PHP Fatal error: Uncaught Error: Class "Mpdf\Mpdf" not found in C:\xampp\htdocs\server\pdf.php:5

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Autoloading Breaks in PHP 8: Fixing the "Class Not Found" Error

If you are running into a PHP Fatal error: Uncaught Error: Class "Mpdf\Mpdf" not found, despite having the correct version of the library installed via Composer, the culprit is almost certainly how your application attempts to load that class. The stack trace points to line 5 in pdf.php, where you instantiate \Mpdf\Mpdf. However, the error message itself tells a different story: the autoloader has failed to map the namespace to a file.

Looking at your provided files, the issue lies in the interaction between PHP's strict namespace handling and your custom autoloading logic. You are using require_once __DIR__ . '/pdf/autoload.php';. This manual inclusion is the red flag. In modern PHP development, especially with Composer-managed dependencies like mpdf v6.1.2, you generally do not need to write a custom autoload.php file or manually require it in every script.

Composer generates an autoloader (vendor/autoload.php) that reads your composer.lock and maps namespaces to their corresponding files in the vendor directory. When you explicitly require a specific autoload.php inside your project root that isn't the standard Composer autoloader, you often bypass the mechanism that actually knows where Mpdf\Mpdf lives. If your custom file is empty or incomplete, PHP simply never loads the class definition, resulting in the "Class not found" error at runtime.

The Solution: Use the Standard Autoloader

The fix is to stop trying to manage the autoloading manually and instead delegate this task to Composer's generated file. Replace your current line in pdf.php:

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

With this line:

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

This path assumes your project structure places the vendor directory at the root level relative to your script. If you are using Laravel or a framework that sits within a specific directory, adjust the path accordingly, but the principle remains: use the autoloader generated by Composer in the root of your package.

Why This Works

The error Uncaught Error: Class "Mpdf\Mpdf" not found is a fatal error because PHP cannot resolve the namespace \Mpd\mpdf. When you use require __DIR__ . '/pdf/autoload.php', you are likely loading a file that does not contain the logic to map the Mpdf namespace to the file located at vendor/mpdf/mpdf/src/Mpdf.php.

Composer's autoloader is dynamic. It checks your composer.lock, sees that mpdf/mpdf version v6.1.2 is installed, and constructs the correct path to the class files on the fly. By bypassing this with a custom require statement, you break the link between the namespace declaration in your code (new \Mpdf\Mpdf) and the actual file system location.

Verifying the Installation

Before switching to the standard autoloader, ensure that Composer has actually downloaded the package. If your composer.lock exists but the vendor/mpdf directory is missing, the lock file might be out of sync with the vendor/ folder. Run:

composer install

Or, if you are on Windows and using XAMPP as indicated by your path (C:\xampp\...), ensure that your terminal is running in a way that allows Composer to write to the vendor directory. Sometimes permission issues or path separators can cause Composer to report success while failing to actually place files.

Namespace Consistency

Once you switch to require __DIR__ . '/../vendor/autoload.php';, ensure that your class instantiation matches the installed package exactly. The stack trace shows the error occurs in pdf.php. Your code uses \Mpdf\Mpdf. This is correct for version 6.x, which moved the namespace from Mpdf to Mpd\mpdf (wait, no, it remains \Mpdf\Mpdf).

Wait, let's double-check the source material. The composer.lock shows name: "mpdf/mpdf". The class is Mpdf\Mpdf. Your code uses \Mpdf\Mpdf. This matches. The problem is purely the missing autoloader logic. If you were using an older version (v5 or earlier), the namespace might have been different, but v6.1.2 definitely expects \Mpdf\Mpdf.

By relying on Composer's autoload, you guarantee that the file mapping is correct regardless of minor internal changes in the library's directory structure. This approach scales better if you add more libraries later; you won't need to update every single PHP file with new require statements for custom autoloaders. Just update composer.json, run composer install, and your code will work immediately.

Final Check

If you still encounter issues after updating the require statement, check that the vendor directory exists in your XAMPP htdocs folder. Sometimes, if you installed the package via a different path or copied files manually without running Composer, the autoloader won't function. Running composer dump-autoload -o is also a good practice to refresh the autoloader map without reinstalling packages, ensuring it picks up any new dependencies correctly.

Our network