Support the ongoing development of Laravel.io →
posted 9 years ago
Blade
Last updated 1 year ago.
0

What is the purpose of the link to the PDF file? Do you simply want to display it or should it be downloadable? And where do you store your PDF files? In the public folder or in a storage folder that is not accessible to the public?

Anyhow, if I may suggest something:

Show

I use an adaption of this to upload and show images and PDF files

http://www.codetutorial.io/laravel-5-file-upload-storage-downl...

You essentially add a the following line of code in your (blade) template

<img src="{{route('getentry', $filename)}}" />

which does not link directly to your image/PDF file, but rather to a controller method 'getentry'. The controller then returns a response with the proper content type and your image/PDF file will be displayed.

Download

This works similar to the method above, but this time, I will do it directly in 'routes.php' and not via a controller. Essentially, you add a download link (either text link or wrap the whole PDF file as a downloadable link) in your template.

<a href="{{ asset('download/' . $filename }}">Download File</a>

The path is at this moment not really important, you may choose whatever you want and replace the 'download'. You will see in a moment why. Important is the proper filename in your link. Next, in your 'routes.php', you will add something like this

Route::get('download/{filename}', function$filename)
{
    $file = storage_path('app') . '/' . $filename; // or wherever you have stored your PDF files
    return response()->download($file);
});

In this example, the files are stored in the ROOT/storage/app/ folder. If you have a link in your template like

<a href="http://mylaravel.com/download/MyPDF.pdf";>Download File</a>

it will use the router above and return a download response.

Hope that helps.

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.