How to display the pdf file in iframe in laravel?

by darrion.kuhn , in category: HTML & CSS , 2 months ago

How to display the pdf file in iframe in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dana , 2 months ago

@darrion.kuhn 

To display a PDF file in an iframe in Laravel, you can use the following steps:

  1. First, you need to store the PDF file in your Laravel project (e.g., in the public directory or storage directory).
  2. In your Blade view file, you can create an iframe element and set its src attribute to point to the URL of your PDF file. For example:
1
<iframe src="{{ asset('path/to/your/pdf/file.pdf') }}" width="100%" height="600"></iframe>


Make sure to replace 'path/to/your/pdf/file.pdf' with the actual path to your PDF file.

  1. If you need to pass the PDF file path from your controller to the Blade view, you can do so by passing it as a variable in your controller method and then accessing it in the Blade view. For example:


In your controller method:

1
2
$pdfFilePath = public_path('path/to/your/pdf/file.pdf');
return view('myview', compact('pdfFilePath'));


In your Blade view:

1
<iframe src="{{ asset($pdfFilePath) }}" width="100%" height="600"></iframe>


  1. If you are storing the PDF file in the storage directory, you can generate a temporary public URL for the file using the Storage facade in Laravel. For example:
1
2
3
4
use IlluminateSupportFacadesStorage;

$pdfFilePath = Storage::url('path/to/your/pdf/file.pdf');
return view('myview', compact('pdfFilePath'));


In your Blade view:

1
<iframe src="{{ $pdfFilePath }}" width="100%" height="600"></iframe>


By following these steps, you should be able to display a PDF file in an iframe in Laravel.