How to Display PDF file in Laravel From Public Folder


How to Display PDF file in Laravel From Public Folder

In this tutorial, we will learn how to display PDF file in laravel from the public folder. This is a very important feature of web development and is often used in web applications. There are various ways to do this in laravel but I have given the simplest way to return a pdf file and showing on the browser. The functions of showing files on the browser are a little bit different in core PHP. Also, you will learn both ways in this tutorial. Please follow the below steps

Steps to display PDF file in laravel

Step 1:- Locate the PDF document inside the laravel public folder of your project.

Also read, How to create laravel project from scratch step by step

Step 2 :- Set up the route in web.php inside the routes folder of your laravel project as shown below

//for displaying PDF
Route::get('/display_pdf','PdfController@index');

Step 3:- Create a controller by using the artisan command as shown below

php artisan make:controller PdfController

PdfController:-

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use File;
use Response;

class PdfController extends Controller
{
    //
    public function index(){
       
         return Response::make(file_get_contents('images/image1.pdf'), 200, [
                        'content-type'=>'application/pdf',
                    ]);
    }
}

Step 4:- Hit the below URL on the browser as shown below

http://127.0.0.1:8000/display_pdf

Now you can see the PDF document as shown below

How to Display PDF file in Laravel From Public Folder

Also read, How to display PDF file in PHP on browser

Conclusion:- I hope this tutorial will help you to understand the overview. If there is any doubt then please leave a comment below


Leave a Reply

Your email address will not be published.

Theme of 96 THEME.