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

Compiling assets with Laravel Mix

Compiling assets with Laravel Mix

Laravel Mix provides a fluent API for defining Webpack build steps
Read more...
Using Laravel's Eloquent ORM Efficiently

Using Laravel's Eloquent ORM Efficiently

Tips & tricks for Laravel's Eloquent ORM.
Read more...
Laravel Scout: Standard way to use algolia search with your Laravel application

Laravel Scout: Standard way to use algolia search with your Laravel application

Algolia search engine within your Laravel application using Laravel Scout.
Read more...