on localhost this package works fine but when I deploy it on live server and I tried to generate pdf I got this error
Class "Barryvdh\DomPDF\Facade" not found"
what was the reason behind this my code is
use BarryvdhDomPDFFacade as PDF;
I got error on this line of code
$pdf = Pdf::loadHTML($htmlContent);
my config/app.php code is
'providers' => ServiceProvider::defaultProviders()->merge([
BarryvdhDomPDFServiceProvider::class,
])->toArray(),
'aliases' => Facade::defaultAliases()->merge([
// 'Example' => AppFacadesExample::class,
'PDF' => BarryvdhDomPDFFacade::class,
])->toArray(),
and on both localhost and live server have php version 8.2
2
Answers
The class you’re looking for is
BarryvdhDomPDFFacadePdf
.Although you don’t really need to create an alias, using the default facade is perfectly fine.
Remember that
Pdf
andPDF
can be 2 different things depending on your system.Use the Correct Class Name: Ensure that you’re importing BarryvdhDomPDFFacadePdf with the correct capitalization. To fix this, add this at the top of your controller or middleware file:
Inject Pdf in the Constructor: Inject the Pdf class into your controller or service to avoid directly using the facade. This approach is particularly helpful for dependency injection and enhances testability.
Check Configurations and Cache: After making these changes, clear and cache your configurations to ensure Laravel recognizes the updated class references:
Example Usage
After these adjustments, you can generate PDFs like so:
Summary