Fede
Fede

Reputation: 1716

Force File (PDF) Download in Laravel 4

I'm trying to force a file download, so when the user click the button, a PDF is generated automatically with data from my DB, and download. I'm using the repository pdf-l4 from thujohn.

Here is my code in the controller

public function downloadPDF($id) {

   $document = Document::find($id);

   $pdf =  '<html><body>'. $document->title . '<br />'. $document->body . '</body></html>';

   return PDF::load($pdf, 'A4', 'portrait')->download($document->title);

}

How can I access that function from my view?

Upvotes: 1

Views: 1685

Answers (1)

Fede
Fede

Reputation: 1716

Done...

DocumentsController.php

public function downloadPDF($id) {

   $document = Document::find($id);

   $pdf =  '<html><body>'. $document->title . '<br />'. $document->body . '</body></html>';

   return PDF::load($pdf, 'A4', 'portrait')->download($document->title);

}

Routes.php

Route::get('pdf/{id}', 'DocumentsController@downloadPDF');

profile.php

   <a href="{{ URL::to('pdf', $document->id )}}" class='btn btn-xs btn-warning'>
  PDF</a>

If there is a better way, please be welcome :)

Upvotes: 1

Related Questions