How to display the pdf file in iframe in laravel?

Member

by darion , in category: PHP Frameworks , 20 days ago

How to display the pdf file in iframe in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by mallory_cormier , 19 days ago

@darion 

To display a PDF file in an iframe in Laravel, you can follow these steps:

  1. Save the PDF file in the storage folder of your Laravel application.
  2. Create a route in your web.php file that will return the PDF file as a response. For example:
1
2
3
4
5
Route::get('/pdf/{filename}', function ($filename) {
    $path = storage_path('app/' . $filename);
    
    return response()->file($path);
});


  1. Create a view file where you want to display the PDF file. In this view file, use an iframe tag to display the PDF file using the route defined in the previous step. For example:
1
<iframe src="{{ route('pdf', ['filename' => 'example.pdf']) }}" width="100%" height="600px"></iframe>


Replace 'example.pdf' with the actual filename of your PDF file.

  1. Make sure your PDF file is stored in the storage folder of your Laravel application and is accessible to be displayed in the iframe.
  2. Access the view file in your browser, and you should see the PDF file displayed in the iframe.


This is a basic example of how to display a PDF file in an iframe in Laravel. You can customize the code based on your specific requirements and use case.