How to Take Browser Screenshots in Laravel?

Hello Artisan,

This tutorial will give you an example of laravel take screenshot. Here you will learn laravel take screenshot of website url. you can understand a concept of how to take website screenshot from url in laravel. we will help you to give an example of how to take a screenshot in laravel. Alright, let us dive into the details.

You can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 versions.

We will use spatie/browsershot composer package for take a screenshot of website in laravel. we will use url(), setOption(), windowSize(), waitUntilNetworkIdle() and save() method to capture browser screenshot in laravel. so let’s follow the below steps:

Step 1 : Install Laravel

first of all we need to get fresh Laravel version application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project laravel/laravel example-app

Step 2: Install spatie/browsershot Package

here, we will install spatie/browsershot package for take a screenshot of url in laravel. so, let’s run the bellow commands:

composer require spatie/browsershot

Next, we need to install puppeteer npm package, that used to capture screenshot. let’s installed using the below command:

npm install puppeteer --global

Step 3: Create Route

In this is step we need to create one route for capture browser screenshot. let’s add the below route on web.php file.

routes/web.php

<?php

use IlluminateSupportFacadesRoute;

use AppHttpControllersDemoController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

Route::get('demo', [DemoController::class,'index']);

Step 4: Create Controller

in this step, we need to create DemoController with index()method.

Add the below code on controller file.

app/Http/Controllers/DemoController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use SpatieBrowsershotBrowsershot;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

Browsershot::url('https://www.itsolutionstuff.com')

->setOption('landscape', true)

->windowSize(3840, 2160)

->waitUntilNetworkIdle()

->save('itsolutionstuff.jpg');

dd("Done");

}

}

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/demo

Output:

How to Take Browser Screenshots in Laravel?

I hope it can help you…

Leave a Comment