spatie / browsershot
Convert a webpage to an image or pdf using headless Chrome
Fund package maintenance!
spatie
Requires
- php: ^8.2
- ext-fileinfo: *
- ext-json: *
- spatie/temporary-directory: ^2.0
- symfony/process: ^6.0|^7.0
Requires (Dev)
- pestphp/pest: ^3.0
- spatie/image: ^3.6
- spatie/pdf-to-text: ^1.52
- spatie/phpunit-snapshot-assertions: ^4.2.3|^5.0
- dev-main
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.4.0
- 4.3.1
- 4.3.0
- 4.2.1
- 4.2.0
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.0
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- v3.x-dev
- 3.61.0
- 3.60.2
- 3.60.1
- 3.60.0
- 3.59.0
- 3.58.2
- 3.58.1
- 3.58.0
- 3.57.8
- 3.57.7
- 3.57.6
- 3.57.5
- 3.57.4
- 3.57.3
- 3.57.2
- 3.57.1
- 3.57.0
- 3.56.0
- 3.55.0
- 3.54.0
- 3.53.0
- 3.52.6
- 3.52.5
- 3.52.4
- 3.52.3
- 3.52.2
- 3.52.1
- 3.52.0
- 3.51.0
- 3.50.2
- 3.50.1
- 3.50.0
- 3.49.0
- 3.48.0
- 3.47.0
- 3.46.0
- 3.45.0
- 3.44.1
- 3.44.0
- 3.42.0
- 3.41.2
- 3.41.1
- 3.41.0
- 3.40.3
- 3.40.2
- 3.40.1
- 3.40.0
- 3.39.0
- 3.38.0
- 3.37.2
- 3.37.1
- 3.37.0
- 3.36.0
- 3.35.0
- 3.34.0
- 3.33.1
- 3.33.0
- 3.32.2
- 3.32.1
- 3.32.0
- 3.31.1
- 3.31.0
- 3.30.0
- 3.29.0
- 3.27.0
- 3.26.3
- 3.26.2
- 3.26.1
- 3.26.0
- 3.25.1
- 3.25.0
- 3.24.0
- 3.23.1
- 3.23.0
- 3.22.1
- 3.22.0
- 3.20.1
- 3.20.0
- 3.19.0
- 3.18.0
- 3.17.0
- 3.16.1
- 3.16.0
- 3.15.0
- 3.14.1
- 3.14.0
- 3.13.0
- 3.12.0
- 3.11.1
- 3.11.0
- 3.10.0
- 3.9.0
- 3.8.1
- 3.8.0
- 3.7.0
- 3.6.0
- 3.5.0
- 3.4.0
- 3.3.1
- 3.3.0
- 3.2.1
- 3.2.0
- 3.1.0
- 3.0.0
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.0
- 2.2.0
- 2.1.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.9.1
- 1.9.0
- 1.8.0
- 1.7.0
- 1.6.0
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.0
- 1.3.0
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.0
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
This package is auto-updated.
Last update: 2024-12-16 09:47:11 UTC
README
Convert a webpage to an image or pdf using headless Chrome
The package 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.
Here's a quick example:
use Spatie\Browsershot\Browsershot; // an image will be saved Browsershot::url('https://example.com')->save($pathToImage);
It 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');
You can also use an arbitrary html input, simply replace the url
method with html
:
Browsershot::html('<h1>Hello world!!</h1>')->save('example.pdf');
If your HTML input is already in a file locally use the :
Browsershot::htmlFromFilePath('/local/path/to/file.html')->save('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
If you wish to retrieve an array list with all of the requests that the page triggered you can do so:
$requests = Browsershot::url('https://example.com') ->triggeredRequests(); foreach ($requests as $request) { $url = $request['url']; //https://example.com/ }
To use Chrome's new headless mode pass the newHeadless
method:
Browsershot::url('https://example.com')->newHeadless()->save($pathToImage);
Support us
Learn how to create a package like this one, by watching our premium video course:
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Documentation
All documentation is available on our documentation site.
Testing
For running the testsuite, you'll need to have Puppeteer installed. Pleaser refer to the Browsershot requirements here. Usually npm -g i puppeteer
will do the trick.
Additionally, you'll need the pdftotext
CLI which is part of the poppler-utils package. More info can be found in in the spatie/pdf-to-text readme. Usually brew install poppler-utils
will suffice.
Finally run the tests with:
composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.
Alternatives
If you're not able to install Node and Puppeteer, take a look at v2 of browsershot, which uses Chrome headless CLI to take a screenshot. v2
is not maintained anymore, but should work pretty well.
If using headless Chrome does not work for you take a look at at v1
of this package which uses the abandoned PhantomJS
binary.
Credits
And a special thanks to Caneco for the logo ✨
License
The MIT License (MIT). Please see License File for more information.