assisted-mindfulness / browsershot
Convert a webpage to an image or pdf using headless Chrome
Installs: 442 569
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- spatie/image: ^2.2
- spatie/phpunit-snapshot-assertions: ^4.0
- spatie/temporary-directory: ^2.1
- symfony/process: ^6.0
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-10-18 12:58:10 UTC
README
The package can convert a webpage to an image or PDF. The conversion is done behind the screens by Google Chrome. This package has been tested on MacOS and Ubuntu. If you use another OS your mileage may vary. Chrome should be installed on your system.
On a Forge provisioned Ubuntu server you can install the latest stable version of Chrome like this:
sudo wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add - sudo sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' sudo apt-get update sudo apt-get -f install sudo apt-get install google-chrome-stable
Also, if necessary, you need to install additional resources, such as fonts for displaying emoji:
sudo apt-get install fonts-noto-color-emoji
Installation
This package can be installed through Composer.
composer require assisted-mindfulness/browsershot
Usage
In all examples it is assumed that you imported this namespace at the top of your file
use AssistedMindfulness\Browsershot\Browsershot;
Here's the easiest way to create an image of a webpage:
Browsershot::url('https://example.com')->save($pathToImage);
Browsershot will make an educated guess where Google Chrome is located. If Chrome can not be found on your system you can manually set its location:
Browsershot::url('https://example.com') ->setChromePath($pathToChrome) ->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);
You can also capture the webpage at higher pixel densities by passing a device scale factor value of 2 or 3. This mimics how the webpage would be displayed on a retina/xhdpi display.
Browsershot::url('https://example.com') ->deviceScaleFactor(2) ->save($pathToImage);
In fact, you can use all the methods spatie/image provides. Here's an example where we create a greyscale image:
Browsershot::url('https://example.com') ->windowSize(640, 480) ->greyscale() ->save($pathToImage);
If, for some reason, you want to set the user agent Google Chrome should use when taking the screenshot you can do so:
Browsershot::url('https://example.com') ->userAgent('My Special Snowflake Browser 1.0') ->save($pathToImage);
The default timeout of Browsershot is set to 60 seconds. Of course, you can modify this timeout:
Browsershot::url('https://example.com') ->timeout(120) ->save($pathToImage);
Browsershot will save a pdf if the path passed to the save
method has a pdf
extension.
// a pdf will be saved Browsershot::url('https://example.com')->save('example.pdf');
Alternatively you can explicitly use the savePdf
method:
Browsershot::url('https://example.com')->savePdf('example.pdf');
Browsershot also can get the body of an html page after JavaScript has been executed:
Browsershot::url('https://example.com')->bodyHtml(); // returns the html of the body
You can also use an arbitrary html input, simply replace the url
method with html
:
Browsershot::html('<h1>Hello world!!</h1>')->save('example.pdf');
License
The MIT License (MIT). Please see License File for more information.
This is a fork of the spatie/browsershot project which does not require Node and Puppeteer to be installed on your web server.