Browsershot: Convert a webpage to an image or PDF using headless Chrome

The folks over at Spatie have just release Browsershot v3:

Browsershot is a PHP package which can convert a webpage to an image or pdf. The conversion is done behind the scenes by Puppeteer which controls a headless version of Google Chrome.

Conversion is easy-peasy, hence this example:

use Spatie\Browsershot\Browsershot;

// an image will be saved
Browsershot::url('https://example.com')
   ->save($pathToImage);

To save to a PDF give the target path a .pdf extension, or use the savePdf() method. Here’s an example with some more manipulations:

Browsershot::html($someHtml)
   ->paperSize($width, $height)
   ->margins($top, $right, $bottom, $left)
   ->save('example.pdf');

Don’t forget to install the required puppeteer:

npm install puppeteer

Browsershot →

Elsewhere , , , ,

Leave a Reply

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