laraveldompdf

Class \"Barryvdh\\DomPDF\\Facade\" not found"


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 Barryvdh\DomPDF\Facade as PDF;

I got error on this line of code

        $pdf = Pdf::loadHTML($htmlContent);

my config/app.php code is

'providers' => ServiceProvider::defaultProviders()->merge([
  Barryvdh\DomPDF\ServiceProvider::class,
 ])->toArray(),
 'aliases' => Facade::defaultAliases()->merge([
        // 'Example' => App\Facades\Example::class,
        'PDF' => Barryvdh\DomPDF\Facade::class,
   ])->toArray(),

and on both localhost and live server have php version 8.2


Solution

  • The class you're looking for is Barryvdh\DomPDF\Facade\Pdf.

    Facade::defaultAliases()->merge([
        'PDF' => \Barryvdh\DomPDF\Facade\Pdf::class,
    ])->toArray(),
    
    use PDF;
    
    $pdf = PDF::loadHTML($htmlContent);
    

    Although you don't really need to create an alias, using the default facade is perfectly fine.

    Facade::defaultAliases()->merge([
        // nothing
    ])->toArray(),
    
    use Barryvdh\DomPDF\Facade\Pdf;
    
    $pdf = Pdf::loadHTML($htmlContent);
    

    Remember that Pdf and PDF can be 2 different things depending on your system.