mozew's avatar
Level 6

Non-static method Barryvdh\DomPDF\PDF::loadView() should not be called statically

I want to export pdf for each report from logined a user.

Look at my codes.

my blade

<a class="btn btn-primary btn-sm" href="{{ route('report.export-pdf') }}">Download PDF</a>

web.php

Route::get('report/export-pdf', 'ReportController@export_pdf')->name('report.export-pdf');

ReportController.php

public function export_pdf(Report $report)
{
    $report =  Report::findOrFail($report);
    $pdf = PDF::loadView('Home.report')->setPaper('a4', 'portrait');
    $fileName = $report->issue_number;
    return $pdf->stream($fileName.'.pdf');
}
0 likes
14 replies
bobbybouwmann's avatar

You probably imported the wrong class or you didn't register the facade itself!

If you added the facade in config/app.php you need to do this at the top of your controller

use PDF;

If you want to point to the specific facade class you need to do this

use Barryvdh\DomPDF\Facade as PDF;
5 likes
bobbybouwmann's avatar

@irankhosravi You can only call static methods on the facade. Otherwise you need to do this:

$pdf = app('dompdf.wrapper');
$pdf->loadView('view');
aurawindsurfing's avatar

@irankhosravi actually call you can call it like this:

$pdf = \App::make('dompdf.wrapper');
$pdf->loadView('invoices.credit_note', compact('credit_notes'));
$pdf->save(public_path($path));

Hope it helps!

Jaytee's avatar

@AURAWINDSURFING - I'm not sure why anyone would want to use that approach in Laravel 5. app() and resolve() are helpers to App::make() and a facade doesn't need to be imported.

Use @bobbybouwmann 's approach, resolving it via the service container using the helper method.

Mahaveer's avatar

@irankhosravi - I'm not sure what is the main problem. but please try this. change your namespace after try.

use Barryvdh\DomPDF;
mozew's avatar
Level 6

@BOBBYBOUWMANN - I get this error.

Undefined variable: order (View: C:\xampp\htdocs\urmiahardware\magzrayaneh\resources\views\Home\report.blade.php)

Then I changed this my codes.

public function export_pdf(Order $order)
{
    $report =  Report::findOrFail($order);
    //$pdf = PDF::loadView('Home.report')->setPaper('a4', 'portrait');
    $pdf = app('dompdf.wrapper');
    $pdf->loadView('Home.report');
    $fileName = $report->issue_number;
    return $pdf->stream($fileName.'.pdf');
}
aurawindsurfing's avatar

@JAYTEE - I hear you but if I remember well it did not work and that was why I did it that way. Not 100% sure tough

bobbybouwmann's avatar

@irankhosravi I'm not sure what you're trying to do here. You dependency inject an order here based on the url, but then want to get a report. This bit of code won't work as well!

Scooby_lahcen's avatar

just use this in controller it will fix the problem : use Barryvdh\DomPDF\Facade\Pdf;

omareee's avatar

fuck anyone else opinion thats the only solution

$pdf = FacadePdf::loadView('admin.pdf'); return $pdf->download('order.details.pdf');

InternetFacilito's avatar

Si estas usando: "use Barryvdh\DomPDF\Facade\Pdf;", deberás usar "Pdf::loadView()".

Si estas usando: "use Barryvdh\DomPDF\Facade\Pdf as PDF;", podrás usar "PDF::loadView()"

Please or to participate in this conversation.