Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AhmedFathy's avatar

Laravel ... view pdf file stored in S3 in new tab

I want to view pdf file stored in S3 in a new tab .. but my code downloaded it

route

Route::resource('legislations', LegislationController::class);

store

  public function store(Request $request)
  {
	 $validated = $request->validate([
        'file'          => 'required',
      ]);

      $name = $request->leg_number . '_' . $request->leg_year . '.pdf';
      Storage::disk('s3')->put($name, file_get_contents($request->file), 'public');
      $validated['leg_path'] = $name;
      Legislation::create($validated);
  }

show

  public function show(Legislation $legislation)
  {
    $content = Storage::disk('s3')->get('documents/' . $legislation->leg_path);

    $header = [
      'Content-Type' => 'application/pdf',
      'Content-Disposition' => 'inline; filename="' . $legislation->leg_path . '"'
    ];

    return Response::make($content, 200, $header);
}

view

    <a href="{{ route('admin.legislations.show', $leg->id) }}" target="_blank"></a>
0 likes
2 replies
Glukinho's avatar

@troccoli A file is downloaded instead of being opened in browser generally in three cases:

  1. html link has "download" attribute:
<a href="https://site.com/storage/file.pdf" download>click me</a>
  1. when responding with data, Content-Disposition header has "attachment" value:
Content-Disposition: attachment; filename="file.pdf"
  1. when responding with data, Content-Type header has value a browser is unable to open/render itself:
Content-Type: application/unknown-type

Please or to participate in this conversation.