Udhayan Nair
Udhayan Nair

Reputation: 552

Laravel 5.4 return a pdf download

basically I have a question,

How do I generate a pdf download on my controller's return. What i mean is, in my controller im fetching data and would like to pass it to my pdf blade view, so on the return part, is it possible to download my pdf?

Generally in a normal blade view, we pass teh data via; return view('admin.log_actions', compact('logs')); for example. So is it possible to set it like return pdf(pdf_route, compact(data)); ?

Upvotes: 0

Views: 1167

Answers (1)

Ozan Kurt
Ozan Kurt

Reputation: 3867

If you don't have the pdf document yet.

Download this package: https://github.com/barryvdh/laravel-dompdf

And use the following code.

$pdf = PDF::loadView('pdfs.my-pdf-view', $data);

return $pdf->download('name.pdf');

If you have the pdf file already, you can use the response()->file() helper.

return response()->file($filePath);

Upvotes: 3

Related Questions