Convert webpage to PDF or image in laravel

Convert webpage to PDF or image in laravel

2019-04-14T16:24:36.000Z

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);