How to Take Browser Screenshots in Laravel

How to Take Browser Screenshots in Laravel

Taking screenshots of browsers is very easy in Laravel applications with the help of spatie/browsershot package. Just install spatie/browsershot in laravel project and use its object browsershot, you can take screenshots of browser.

How to Take Browser Screenshot in Laravel using spatie/browsershot Package

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

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

Navigate to routes directory and open web.php file, then create one route for capturing 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

To create a controller class, simply run php artisan make:controller TestController command to create it:

php artisan make:controller TestController

Navigate to controller folder/directory and open testcontroller.php and update the following function 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 the 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

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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