Convert webpage to PDF or image in laravel

Convert webpage to PDF or image in laravel

Apr 11, 2019 / Ramesh Mhetre

It's not much common requirement to convert a webpage to PDF or image but sometimes it is required than never.

Spatie has a nice package called Browsershot to convert webpage to either image or PDF. Under the hood this conversion is made possible with headless chrome option.

In order to make use of Browsershot you must make sure that Chrome 59 or higher is installed on your system.

After installing package as per the installation guide, you can use the package like:

Browsershot::url('https://example.com')->save($pathToImage);

By default the screenshot will be a png and it's size will match the resolution you use for your desktop. Want another size of screenshot? No problem!

Browsershot::url('https://example.com')
    ->windowSize(640, 480)
    ->save($pathToImage);

You can also set the size of the output image independently of the size of window. Here's how to resize a screenshot take with a resolution of 1920x1080 and scale that down to something that fits inside 200x200.

Browsershot::url('https://example.com')
    ->windowSize(1920, 1080)
    ->fit(Manipulations::FIT_CONTAIN, 200, 200)
    ->save($pathToImage);

Similar Articles

What is new to Laravel 6?

What is new to Laravel 6?

Today Laravel 6 has finally officially released. Let's have a detailed look at what is changing in Laravel 6.
Read more...
Connect your laravel application to Sqlite DB

Connect your laravel application to Sqlite DB

Quickly connecting with sqlite database within laravel application
Read more...
Laravel v6 & Laravel Vapor is going to release soon 🚀

Laravel v6 & Laravel Vapor is going to release soon 🚀

Laravel v6 is is releasing soon. Serverless laravel is next big thing in laravel community!
Read more...