Reputation: 31
I'm trying to use dompdf
with Laravel, even though I use the 'use PDF' it's still error.
Here is my controller
<?php
namespace App\Http\Controllers;
use RealRashid\SweetAlert\Facades\Alert;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Pasien;
use Illuminate\Support\Facades\Redirect;
use PDF;
class PasienController extends Controller {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
$pasien = DB::table('pasien')->paginate(5);
return view('pasien/index', ['pasien' => $pasien]);
}
public function createPDF() {
// retreive all records from db
$data = Pasien::all();
// share data to view
view()->share('employee',$data);
$pdf = PDF::loadView('pdf_view', $data);
// download PDF file with download method
return $pdf->download('pdf_file.pdf');
}
}
The PDF in this line is red
$pdf = PDF::loadView('pdf_view', $data);
The error display this message
Undefined type 'PDF'
I've put this in the providers
Barryvdh\DomPDF\ServiceProvider::class,
And this on the aliases
'PDF' => Barryvdh\DomPDF\Facade::class,
I've installed the package using composer, and also php artisan vendor publish it.
Upvotes: 1
Views: 16109
Reputation: 21
None of these worked in my case. This worked for me, use 'FacadePdf' instead of 'PDF'. I am using Laravel 9.52.7
use Barryvdh\DomPDF\Facade\Pdf as FacadePdf;
$pdf = FacadePdf::loadView('invoice.inv_pdf', [
'order_id' => $order,
]);
Upvotes: 0
Reputation: 61
If someone is still stucking, I got same issue, and none of solution helped, for Laravel 9.
This works for me:
use Barryvdh\DomPDF\Facade\Pdf;
$pdf = Pdf::loadView('pdf.invoice', $data);
return $pdf->download('invoice.pdf');
Instead of using this, Use this with App Container:
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1>');
return $pdf->stream();
Upvotes: 0
Reputation: 11
In Laravel 8 and above.
INSTEAD OF
'PDF' => Barryvdh\DomPDF\Facade::class,
USE
'PDF' => Barryvdh\DomPDF\Facade\Pdf::class,
INSIDE YOUR CONTROLLER:
use Illuminate\Support\Facades\File;
$pdf = Pdf::loadView('pdf_view', $data);
Upvotes: 1
Reputation: 1231
I just added Barryvdh\DomPDF\ServiceProvider::class
in my providers array (inside <path_your_project>/config/app.php) and 'PDF' => Barryvdh\DomPDF\Facade::class
in my aliases array (inside the same file) and it worked for me!
Note1: I'm using Laravel 8.0.3
Note2: don't forget of run this:
composer require barryvdh/laravel-dompdf
Upvotes: 0
Reputation: 31
Maulik answer combined with Khalid khan really helped me, and it's actually worked!
INSTEAD OF
use PDF;
USE THIS(Even though I heard you can just use, 'use PDF'. But hey it works!)
use Barryvdh\DomPDF\PDF;
And then enter this command
php artisan config:cache
You can try clearing the cache first and then modifying your code, or one way another.
Upvotes: 2
Reputation: 1050
In mine case it works
$pdf = \PDF::loadView('...');
I have added \
before PDF
class name with loadView
.
A \
before the beginning of a class name represents the Global Namespace.
Upvotes: 0