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);
Laravel Packages Laravel Laravel News

Similar Articles

PHP develop your own package with local composer environment

PHP develop your own package with local composer environment

Developing your own PHP composer package locally & integrating local file symlink within your application can speeds up development immensely.
Read more...
Laravel Zero: A Micro-framework for console applications

Laravel Zero: A Micro-framework for console applications

Get leverage of laravel magic to create console applications within a micro framework.
Read more...
What is new in Laravel 5.8.4

What is new in Laravel 5.8.4

Here is what is new in Laravel v5.8.4
Read more...