Convert webpage to PDF or image in laravel

Convert webpage to PDF or image in laravel

Apr 11, 2019 / Moody Developer

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

Moving existing PHPUnit tests to PEST in Laravel application

Moving existing PHPUnit tests to PEST in Laravel application

How to move existing PHPUnit tests to PEST within your Laravel application.
Read more...
How to do multi tenant application with Laravel

How to do multi tenant application with Laravel

How to do multi tenant application with Laravel. Have single laravel setup for multiple applications
Read more...
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...