How to fix Class 'PDF' not found in laravel
Asked Answered
D

10

6

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?

Draw answered 7/12, 2016 at 9:28 Comment(3)
what is your PDF class location?Mccreary
laravel config/app.phpDraw
In your controller header, use this: use Barryvdh\DomPDF\Facade\PDF;Unassuming
A
12

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');
Attu answered 7/12, 2016 at 9:36 Comment(1)
Did you run composer install after adding DomPDF to your composer.json? Or did you ran composer require dompdf/dompdf?Attu
I
11

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));
Ilo answered 7/9, 2018 at 9:39 Comment(1)
I am using lumen for my app's APIs. could you please suggest me how can i use Barryvdh\DomPDF in lumen api to generate and store pdf file. Thanks a lot in advance.Normandnormandy
S
5

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.

Sarraceniaceous answered 28/10, 2022 at 9:17 Comment(0)
T
4

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

Touslesmois answered 9/5, 2017 at 11:16 Comment(0)
I
3

Run the below command

composer require barryvdh/laravel-dompdf
Indication answered 15/10, 2018 at 3:10 Comment(0)
S
2

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;
Sienna answered 29/8, 2023 at 8:23 Comment(1)
replace the first line with the second line? what do you mean?Peroxy
D
0

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

Draw answered 8/12, 2016 at 5:33 Comment(1)
domPDF ki jinis ?Stinko
L
0

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 ]

Littman answered 12/3, 2019 at 7:37 Comment(0)
A
-1

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;

Annapolis answered 5/9, 2024 at 0:10 Comment(0)
B
-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

Bluepencil answered 9/10, 2024 at 17:3 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Nl

© 2022 - 2025 — McMap. All rights reserved.