Fernando
Fernando

Reputation: 411

How to fix Class 'PDF' not found in laravel

Encountered the following error in My Laravel app:

FatalErrorException in CollaboPDFController.php line 14: Class 'PDF' not found

This is my CollaboPDFController.php:

<?php

  namespace App\Http\Controllers;

  use Illuminate\Http\Request;

  use App\Http\Requests;
  use App\Http\Controllers\Controller;
  use PDF;

  class CollaboPDFController extends Controller
  {
      public function getPDF(){
        $pdf = PDF::loadView('customer.customer'); //line 14
        return $pdf->download('customer.customer');
      }
      //
  }

How I can fix this?

Upvotes: 6

Views: 66420

Answers (10)

ahmed saeed
ahmed saeed

Reputation: 1

I faced the problem now and I had to install the library (composer require barryvdh/laravel-dompdf) and I used use Barryvdh\DomPDF\Facade\Pdf; Instead of use PDF

Upvotes: -1

user110288
user110288

Reputation: 9

With the Laravel project, the error described above occurred when declaring Barryvdh\DomPDF\Facade\Pdf directly; adding as fixes the error.

use Barryvdh\DomPDF\Facade\Pdf as PDF;

Upvotes: -1

user22463904
user22463904

Reputation: 39

for those who tried all the solutions mentioned above and nothing works ! Try this it worked for me. In the controller replace

use Barryvdh\DomPDF\Facade as PDF;

by

use Barryvdh\DomPDF\Facade\Pdf as PDF;

Upvotes: 3

am_web_pro_5.0
am_web_pro_5.0

Reputation: 51

In my case, I tried some of the examples above, but nothing worked. However, I browsed the directory of the package located here:

project-folder>vendor>barryvdh>laravel-dompdf>src>Facade>Pdf.php and I realized that the PDF alias created in the file >> app.php does not use the correct class. So replace:

'aliases' => [
  .....
 'PDF' => Barryvdh\DomPDF\Facade::class,
]

by:

'aliases' => [
  .....
 'PDF' => Barryvdh\DomPDF\Facade\Pdf::class,
]

I hope this will help you.

Upvotes: 5

Pooran Singh
Pooran Singh

Reputation: 1

this is problem with cache config.php within bootstrap folder.

shared hosting does not show full path of your [project folder]in shared hosting and if you want clear cache , you have to purchase their plugins. by the given solution you have no need to purchase any plugin or no need to clear / config cache of laravel 5.6 or more.

hit the URl in your browser it will give you error log. now oprn your error log and copy the path from the error log.

change all file paths in config.php of [your project folder ]/bootstrap/cache, [your project folder ]\bootstrap\cache or [your project folder ]* etc by the full path copied from error log like /mnt/stor2-wc1-dfw1/411797/622471/[yourdomain]/web/content/[your project folder ]

Upvotes: 0

shavindip
shavindip

Reputation: 622

Run the below command

composer require barryvdh/laravel-dompdf

Upvotes: 3

Rahul Pawar
Rahul Pawar

Reputation: 1036

First you have to require DOMPDF package

Step 1 :

composer require barryvdh/laravel-dompdf

Step 2 : In ..\config\app.php

'providers' => [ 
  .....
  Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
  .....
 'PDF' => Barryvdh\DomPDF\Facade::class,
]

Step 3 :

use \PDF;

Step 4 :

$pdf = PDF::loadView('pdf.report');
return $pdf->stream('report.pdf', array('Attachment' => 0));

Upvotes: 11

Ramv V
Ramv V

Reputation: 227

You can run following commands after installing dompdf and set service provider and aliases in this file config/app.php

php artisan cache:clear and php artisan config:cache

Upvotes: 4

Fernando
Fernando

Reputation: 411

finally got the solutions actually it was My domPDF installation problem I find out it this way

php artisan cache:clear
// and
php artisan config:cache

and install domPDF again

Upvotes: 0

Bart Bergmans
Bart Bergmans

Reputation: 4121

You are using the wrong import. To use the PDF you want (probably laravel-dompdf) use:

use Barryvdh\DomPDF\Facade as PDF;

If you put 'PDF' => Barryvdh\DomPDF\Facade::class, in your config/app.php you could also use:

use \PDF;

or

\PDF::loadView('customer.customer');

Upvotes: 13

Related Questions