4

I'm working on a web application using Laravel 5.8, I'm new to Laravel framework. I would like to display PDF documents on the browser when users click on some buttons. I will allow authenticated users to "View" and "Download" the PDF documents.

I have created a Controller and a Route to allow displaying of the documents. I'm however stuck because I have a lot of documents and I don't know how to use a Laravel VIEW to display and download each document individually.

/* PDFController*/

public function view($id)
{
    $file = storage_path('app/pdfs/') . $id . '.pdf';

    if (file_exists($file)) {

        $headers = [
            'Content-Type' => 'application/pdf'
        ];

        return response()->download($file, 'Test File', $headers, 'inline');
    } else {
        abort(404, 'File not found!');
    }
}

}

/The Route/ Route::get('/preview-pdf/{id}', 'PDFController@view');

5
  • You want to embed the file to your blade view? Commented Sep 27, 2019 at 17:03
  • Are you wanting to implement this yourself, or would you be open to answers that make use of existing packages? Commented Sep 27, 2019 at 17:05
  • @Mateus - I have the PDF files in the directory storage/app/pdfs. I would like users to view and download these documents. I'm confused on how to use blade and property routing for this to work correctly. Commented Sep 27, 2019 at 17:13
  • Do you have your file paths stored in database or something like that? Commented Sep 27, 2019 at 17:14
  • @Vince - If you know any package that can solve this problem you can share. Thanks. Commented Sep 27, 2019 at 17:14

4 Answers 4

6

Mateus' answer does a good job describing how to setup your controller function to return the PDF file. I would do something like this in your /routes/web.php file:

Route::get('/show-pdf/{id}', function($id) {
    $file = YourFileModel::find($id);
    return response()->file(storage_path($file->path));
})->name('show-pdf');

The other part of your question is how to embed the PDF in your *.blade.php view template. For this, I recommend using PDFObject. This is a dead simple PDF viewer JavaScript package that makes embedding PDFs easy.

If you are using npm, you can run npm install pdfobject -S to install this package. Otherwise, you can serve it from a CDN, or host the script yourself. After including the script, you set it up like this:

HTML:

<div id="pdf-viewer"></div>

JS:

<script>
PDFObject.embed("{{ route('show-pdf', ['id' => 1]) }}", "#pdf-viewer");
</script>

And that's it — super simple! And, in my opinion, it provides a nicer UX for your users than navigating to a page that shows the PDF all by itself. I hope you find this helpful!


UPDATE: After reading your comments on the other answer, I thought you might find this example particularly useful for what you are trying to do.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks Vince and Mateus for your help, let me work this, I appreciate your help.
Glad I could help! 👊🏻
@Vince - To use this code means I will have to create a Model and my PDFs will be fetched from the Database?
Not at all -- just an assumption I made.
@Amos, if this answer solved your problem, please upvote it, and mark it as "accepted" (checkmark) so that it can help others in the future. Thanks!
|
0

According to laravel docs:

The file method may be used to display a file, such as an image or PDF, directly in the user's browser instead of initiating a download.

All you need to do is pass the file path to the method:

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

If you need custom headers:

return response()->file($pathToFile, $headers);

3 Comments

Thanks, I have this code already, but the challenge is how to use the code in my PDFController and create a Blade View that will display different PDF documents on the browser (without users having to leave the dashboard) These different documents will be displayed when the user clicks on a button. For example if the user clicks a button named "Test PDF", they will be able to view the document on the browser.
You can use $files = Storage::allFiles($directory); to list all files inside some directory. Maybe it helps
@Amos see my answer for a suggestion of how to display your PDFs in your blade view
0
Route::get('/show-pdf/{id}', function($id) {
    $file = YourFileModel::find($id);
    return response()->file(storage_path($file->path));
})->name('show-pdf');

Or if file is in public folder

Route::get('/show-pdf', function($id='') {
    return response()->file(public_path().'pathtofile.pdf');
})->name('show-pdf');

then show in page using

<embed src="{{ route('show-pdf') }}" type="text/pdf" >

Comments

0

I got the PDF to download from local storage using the following.

use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;

// the Document controller,...
public function open(Document $document)
    {
        $pathname = storage_path('app/uploads/'.$document->filename); 
        if(File::exists($pathname))
        {
            return response()->file(
                $pathname
            );
        }
        else 
        {
            return redirect()
                ->route('documents.index')
                ->with('failure', 'Failure opening document');
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.