How to fix Class 'PDF' not found in laravel
Encountering a FatalErrorException stating "Class 'PDF' not found" usually signals a namespace collision or an outdated import pattern. In your specific case, the error originates from CollaboPDFController.php, where you are attempting to generate a PDF using PDF::loadView. The root cause is likely that you are importing a class named PDF from an external vendor package (such as barryvdh/laravel-dompdf) without fully understanding how Laravel's autoloading and namespace resolution interact with it.
When you write use PDF; at the top of your controller, you are telling PHP to import a global class or a specific namespaced class into your current scope. If the package you installed defines its class as \Barryvdh\DomPDF\Facade\Pdf (or similar), importing it simply as PDF creates a conflict if that name is not explicitly defined in the vendor's namespace structure exactly as expected. More often than not, modern Laravel versions and newer PDF packages expect you to use the fully qualified facade name rather than a generic short alias.
To resolve this immediately, inspect your composer.json file. You should verify if you have installed a package like barryvdh/laravel-dompdf. If you have, the correct usage within your controller is to replace the generic use PDF; with the specific facade import provided by that package. The corrected code for your controller should look like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade\Pdf; // Use the fully qualified facade name
class CollaboPDFController extends Controller
{
public function getPDF()
{
$pdf = Pdf::loadView('customer.customer');
return $pdf->download('customer.pdf');
}
}
Notice the change from use PDF; to use Barryvdh\DomPDF\Facade\Pdf;. This ensures PHP knows exactly which class you are referring to, bypassing any ambiguity in the autoloader. Additionally, I updated the download filename slightly to avoid potential issues with file extensions containing dots, though ->download('customer.customer') would technically work if your server configuration allows it.
If you are not using the barryvdh/laravel-dompdf package but rather dompdf/dompdf directly, the implementation changes entirely. In that scenario, you cannot use the PDF facade because the vanilla DomPDF library does not provide one out of the box in a way that integrates seamlessly with Laravel's Facade pattern without extra configuration. You would need to instantiate the renderer manually:
use DOMPDF\DOMPDF;
class CollaboPDFController extends Controller
{
public function getPDF()
{
$dompdf = new DOMPDF();
$dompdf->loadHtml(view('customer.customer')->render());
$dompdf->setPaper('a4');
$dompdf->render();
return response($dompdf->output(), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="customer.pdf"'
]);
}
}
This approach is more verbose and requires handling the output stream manually. It lacks the concise syntax of the Facade provided by Barryvdh's package. For most Laravel applications, sticking to the barryvdh/laravel-dompdf package is the standard recommendation because it abstracts away the boilerplate code associated with rendering HTML to PDF.
Another common pitfall involves the view path. The code $pdf->loadView('customer.customer') expects a Blade template located at resources/views/customer/customer.blade.php. If this file does not exist or contains syntax errors, you will see different exceptions later in the stack trace. Ensure your view exists and that it references data passed from your model or controller context correctly.
Finally, always run composer dump-autoload after installing or updating PDF packages to ensure Laravel's class map is updated. Skipping this step can lead to "Class not found" errors even when the package is installed correctly, as the autoloader fails to locate the new facade definitions in your application cache.