1

I need to show pdf online in the user browser, but I couldn't make it works.

My controller :

public function showJournal($file) {
        $filepath = Storage::disk('jurnal')->get($file);

        return (new Response($filepath, 200))
                        ->header('Content-Type', 'application/pdf');
    }

The route :

Route::get('view_journal/{file}', 'JournalController@showJournal')->name('showjurnal');

The view :

<iframe src="{{route('showjurnal', $journal->file)}}" frameborder="0" style="width:100%;min-height:640px;"></iframe>

The codes above doesn't work as it always returning error NotFoundHttpException in RouteCollection.php line 161:. The file is stored in public/jurnal. How do I solve this error ? thanks

Ariando Miller
  • 1,261
  • 3
  • 18
  • 40
  • If you give correct file path then you can display file using get to obtain file path from request – Sagar Gautam Jul 23 '17 at 08:28
  • `NotFoundHttpException` is for invalid route which doesn't exists in route file `web.php` so please check route exist there or not – Sagar Gautam Jul 23 '17 at 08:30
  • I store the file in `public/jurnal/text.pdf`. How do I retrieve the correct path ? @SagarGautam – Ariando Miller Jul 23 '17 at 08:59
  • 1
    I think you have stored filename in database so you can use `asset($filepath)` to get correct path of the file. `asset() ` provides path up to `public/` directory. So , you can try `src="{{asset('/jurnal/text.pdf')}}"` it should work – Sagar Gautam Jul 23 '17 at 09:03
  • Yes it works but will force-download the file instead of opening it. @SagarGautam – Ariando Miller Jul 23 '17 at 09:05
  • You are returning response that's the reason for force-download so just return filepath as variable then display using `` tag. – Sagar Gautam Jul 23 '17 at 09:07

3 Answers3

4

use file responses available from laravel 5.2 and above

return response()->file($pathToFile);
Arun pandian M
  • 735
  • 6
  • 16
  • check file in the `$filepath` is readable and also check the path is correct – Arun pandian M Jul 23 '17 at 08:07
  • I store the file in `public/jurnal/text.pdf`. How do I retrieve the correct path ? – Ariando Miller Jul 23 '17 at 09:00
  • 1
    try this `Storage::url($filename)` File name is `jurnal/text.pdf` or set public/jurnal as the default path in `config/filesystems.php` refer : https://stackoverflow.com/questions/28964412/how-to-get-file-path-using-storage-facade-in-laravel-5 – Arun pandian M Jul 23 '17 at 15:12
0

Store the file not in the public folder, if you want to use Storage. Store the file under storage/app/jurnal/text.pdf for the default Storage Driver. And then you can get the file with

return response()->download( Storage::getDriver()->getAdapter()->applyPathPrefix("jurnal/text.pdf");

Benni
  • 110
  • 9
0

Just change

<iframe src="{{route('showjurnal', $journal->file)}}" frameborder="0" style="width:100%;min-height:640px;"></iframe>

To

<iframe src="{{ url(Storage::disk('jurnal')->url($journal->file)) }}" frameborder="0" style="width:100%;min-height:640px;"></iframe>
linktoahref
  • 6,570
  • 3
  • 20
  • 45