Laravel 08-Feb-2023

How to Take Browser Screenshots in Laravel

Laravel take/make browser screenshots; Through this tutorial, you will learn how to take/make browser screenshots in laravel using browsershot package.

How to Take Browser Screenshots in Laravel

Use the following steps to make/take browser screenshots in laravel:

  • Step 1 – Install Laravel
  • Step 2 – Install Browsershot Package
  • Step 3 – Create Route
  • Step 4 – Create Controller using Artisan
  • Step 5 – Run Laravel App

Step 1 – Install Laravel

First of all, execute the following command on terminal to install laravel app latest version:

composer create-project laravel/laravel blog-example

Step 2 – Install Browsershot Package

Then execute the following command on terminal to install browsershot package in laravel apps:

composer require spatie/browsershot
npm install puppeteer --global

Step 3 – Create Route

Now, visit routes directory and open web.php file. And create one route for capture browser screenshot:

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\TestController;
  
/*
|--------------------------------------------------------------------------
| 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('test', [TestController::class,'index']);

Step 4 – Create Controller using Artisan

Execute the following command on terminal to create controller class in laravel apps:

php artisan make:controller TestController

After that, open controller folder/directory and open testcontroller.php. And add the folowing code into it:

<?php
 
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Spatie\Browsershot\Browsershot;
  
class TestController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        Browsershot::url('https://codinghelptech.com')
            ->setOption('landscape', true)
            ->windowSize(3840, 2160)
            ->waitUntilNetworkIdle()
            ->save('tutsmake.jpg');
  
        dd("Done");
    }
}

Step 5 – Run Laravel App

Execute the following command on temrinal to start laravel apps:

php artisan serve

Now, Go to your web browser, hit the following URL into it:

http://localhost:8000/demo

Conclusion

That’s all; Through this tutorial, you have learned how to take/make browser screenshots in laravel using browsershot package.