- Notifications
You must be signed in to change notification settings - Fork505
Convert HTML to an image, PDF or string
License
spatie/browsershot
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The package can convert a webpage to an image or pdf. The conversion is done behind the scenes byPuppeteer which controls a headless version of Google Chrome.
Here's a quick example:
useSpatie\Browsershot\Browsershot;// an image will be savedBrowsershot::url('https://example.com')->save($pathToImage);
It will save a pdf if the path passed to thesave
method has apdf
extension.
// a pdf will be savedBrowsershot::url('https://example.com')->save('example.pdf');
You can also use an arbitrary html input, simply replace theurl
method withhtml
:
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 ($requestsas$request) {$url =$request['url'];//https://example.com/}
To use Chrome's newheadless mode pass thenewHeadless
method:
Browsershot::url('https://example.com')->newHeadless()->save($pathToImage);
Learn how to create a package like this one, by watching our premium video course:
We invest a lot of resources into creatingbest in class open source packages. You can support us bybuying 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 onour contact page. We publish all received postcards onour virtual postcard wall.
All documentation is availableon our documentation site.
For running the testsuite, you'll need to have Puppeteer installed. Pleaser refer to the Browsershot requirementshere. Usuallynpm -g i puppeteer
will do the trick.
Additionally, you'll need thepdftotext
CLI which is part of the poppler-utils package. More info can be found in in thespatie/pdf-to-text readme. Usuallybrew install poppler-utils
will suffice.
Finally run the tests with:
composertest
Please seeCONTRIBUTING for details.
If you've found a bug regarding security please mailsecurity@spatie.be instead of using the issue tracker.
If you're not able to install Node and Puppeteer, take a look atv2 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 atv1
of this package which uses the abandonedPhantomJS
binary.
And a special thanks toCaneco for the logo ✨
The MIT License (MIT). Please seeLicense File for more information.
About
Convert HTML to an image, PDF or string