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

Khudadad's avatar

How to View a PDF file in the browser

I want to make a link to a pdf file, when visitor clicks on it, it should be viewed in the browser instead of download, but can't and I'm getting this error:

FileNotFoundException in File.php line 37: The file "{"id":3,"title":"sdfdf","isbn":"2323","author":"sfsf","description":null,"size":203932,"book_path":"books\/2015_EX_1.pdf","user_id":2,"category_id":0,"created_at":"2015-11-17 02:04:54","updated_at":"2015-11-17 02:04:54"}" does not exist

This is my function:

public function readBook($id)
{
    $file = Book::findOrFail($id);
    if (File::isFile($file))
    {
        $file = File::get($file);
        $response = Response::make($file, 200);

        $response->header('Content-Type', 'application/pdf');

        return $response;
    }
}

any help thanks

0 likes
5 replies
ohffs's avatar

Should it maybe be File::get($file->book_path) ?

Khudadad's avatar

Thanks for the reply, but still same error

ohffs's avatar

I think the isFile call also expects a path rather than a model. Is the book_path inside your storage directory? Could be you have to provide the full path (ie, something like storage_path($file->book_path) .

bashy's avatar

If you store it in public folder, use public_path('/files/file.pdf') as well. You will need the full path to the file. You can also use storage_path('/documents/file.pdf') if you store it in storage folder.

Use it within File::get() and File::isFile()

Can't remember if it needs the first slash.

Khudadad's avatar

Thanks @basy and @ohffs, but the problem is an extra method in the BookContrller which it's route is not registered in the route.php. Of course I registered the route but is shown when I run php artisan route:list

So, how do I register more routes for an extra methods in a controller, because by default a controller has 8 methods when we create using artisan command. thanks

Please or to participate in this conversation.