Take Browser Screenshot in Laravel using spatie/browsershot Package

Take Browser Screenshot in Laravel using spatie/browsershot Package

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 Screenshot in Laravel using spatie/browsershot Package

Steps to make/take browser screenshots in Laravel using browsershot:

  • Step 1 – Setup New Laravel Project
  • Step 2 – Install spatie/browsershot Package
  • Step 3 – Create Route
  • Step 4 – Create Controller using Artisan
  • Step 5 – Run Laravel App

Step 1 – Setup New Laravel Project

First of all, execute the following command on terminal or cmd to install new laravel new with latest version:

composer create-project laravel/laravel blog-example

Step 2 – Install spatie/browsershot Package

Then execute the following command on the terminal to install spatie/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://www.tutsmake.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 spatie/browsershot package.

Recommended Laravel Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *