skip to Main Content

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


  1. The class you’re looking for is BarryvdhDomPDFFacadePdf.

    Facade::defaultAliases()->merge([
        'PDF' => BarryvdhDomPDFFacadePdf::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 BarryvdhDomPDFFacadePdf;
    
    $pdf = Pdf::loadHTML($htmlContent);
    

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

    Login or Signup to reply.
  2. 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:

    use BarryvdhDomPDFFacadePdf;
    

    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.

    protected $pdf;
    
    public function __construct(Pdf $pdf)
    {
        $this->pdf = $pdf;
    }
    

    Now, you can use $this->pdf to access Pdf methods within the class.

    Check Configurations and Cache: After making these changes, clear and cache your configurations to ensure Laravel recognizes the updated class references:

    php artisan config:clear
    php artisan cache:clear
    php artisan config:cache
    

    Example Usage
    After these adjustments, you can generate PDFs like so:

    public function generatePdf()
    {
       $pdf = Pdf::loadView('pdf.table', compact(
                   'data'
                ));
                $pdf->setPaper('A4', 'portrait');
    
                return $pdf->download('data_table_12.pdf');
    }
    

    Summary

    The key fix is to ensure BarryvdhDomPDFFacadePdf is imported
    correctly with case sensitivity in mind. Injecting it into the
    constructor also allows Laravel to manage the dependency, reducing
    issues on case-sensitive filesystems.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search