How To Take Screenshots In Laravel 9 Example

Hello Artisan,

In this tutorial, I will show you how to make a screenshot in laravel application. For taking screenshots, I will use spatie/browsershot package. If you are looking for the screenshot-taking tutorial in Laravel, then you are in the right place.

I will show you step by step guide on how to make a screenshot in laravel. Browser shot is software by the Spatie team that converts a webpage into an image or PDF by utilizing a headless instance of Chrome.

See the preview:

 

Step 1 : Install Laravel

I am going to start from scratch. So download a fresh Laravel project using the below command:

composer create-project laravel/laravel example-app

 

Step 2: Install Browsershot

The installation is pretty straightforward. Go to your Laravel root directory and enter the following command:

composer require spatie/browsershot

 

There is one more software we need that is called Puppeteer:

npm install puppeteer --global

 

Step 3: Create Route

In this step, we will create the route. Update the below file like that:

routes/web.php

use App\Http\Controllers\BrowsershotController;

Route::get('/test-screenshot', [BrowsershotController::class, 'screenshotGoogle']);

 

 

Step 4: Create Controller

To use Browsershot, we first need to create a controller. Create a controller and update it like this:

app\Http\Controllers\BrowsershotController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Spatie\Browsershot\Browsershot;

class BrowsershotController extends Controller
{
    public function screenshotGoogle() {

    Browsershot::url('https://www.codecheef.org')
            ->setOption('landscape', true)
            ->windowSize(3840, 2160)
            ->waitUntilNetworkIdle()
            ->save("storage/" . 'codecheef.jpg');
   }

}

 

#laravel #laravel-9x