Download the PHP package info2soft/browsershot without Composer
On this page you can find all versions of the php package info2soft/browsershot. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download info2soft/browsershot
More information about info2soft/browsershot
Files in info2soft/browsershot
Package browsershot
Short Description Convert a webpage to an image or pdf using headless Chrome
License MIT
Homepage https://github.com/spatie/browsershot
Informations about the package browsershot
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:
It will save a pdf if the path passed to the save
method has a pdf
extension.
You can also use an arbitrary html input, simply replace the url
method with html
:
Browsershot also can get the body of an html page after JavaScript has been executed:
Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
Requirements
This package requires node 7.6.0 or higher and the Puppeteer Node library.
On MacOS you can install Puppeteer in your project via NPM:
Or you could opt to just install it globally
On a Forge provisioned Ubuntu 16.04 server you can install the latest stable version of Chrome like this:
Custom node and npm binaries
Depending on your setup, node or npm might be not directly available to Browsershot.
If you need to manually set these binary paths, you can do this by calling the setNodeBinary
and setNpmBinary
method.
By default, Browsershot will use node
and npm
to execute commands.
Custom include path
If you don't want to manually specify binary paths, but rather modify the include path in general,
you can set it using the setIncludePath
method.
Setting the include path can be useful in cases where node
and npm
can not be found automatically.
Custom node module path
If you want to use an alternative node_modules
source you can set it using the setNodeModulePath
method.
Custom binary path
If you want to use an alternative script source you can set it using the setBinPath
method.
Custom chrome/chromium executable path
If you want to use an alternative chrome or chromium executable from what is installed by puppeteer you can set it using the setChromePath
method.
Pass custom arguments to Chromium
If you need to pass custom arguments to Chromium, use the addChromiumArguments
method.
The method accepts an array
of key/value pairs, or simply values. All of these arguments will automatically be prefixed with --
.
If no key is provided, then the argument is passed through as-is.
Example array | Flags that will be passed to Chromium |
---|---|
['foo'] |
--foo |
['foo', 'bar'] |
--foo --bar |
['foo', 'bar' => 'baz' ] |
--foo --bar=baz |
This method can be useful in order to pass a flag to fix font rendering issues on some Linux distributions (e.g. CentOS).
Installation
This package can be installed through Composer.
Usage
In all examples it is assumed that you imported this namespace at the top of your file
Screenshots
Here's the easiest way to create an image of a webpage:
Formatting the image
By default the screenshot's type will be a png
. (According to Puppeteer's Config)
But you can change it to jpeg
with quality option.
Sizing the image
By default the screenshot's size will match the resolution you use for your desktop. Want another size of screenshot? No problem!
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.
You can screenshot only a portion of the page by using clip
.
You can take a screenshot of an element matching a selector using select
.
Manipulating the image
You can use all the methods spatie/image provides. Here's an example where we create a greyscale image:
Taking a full page screenshot
You can take a screenshot of the full length of the page by using fullPage()
.
Setting the device scale
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.
Mobile emulation
You can emulate a mobile view with the mobile
and touch
methods.
mobile
will set the display to take into account the page's meta viewport, as Chrome mobile would.
touch
will set the browser to emulate touch functionality, hence allowing spoofing for pages that check for touch.
Along with the userAgent
method, these can be used to effectively take a mobile screenshot of the page.
Device emulation
You can emulate a device view with the device
method. The devices' names can be found Here.
is the same as
Backgrounds
If you want to ignore the website's background when capturing a screenshot, use the hideBackground()
method.
Dismiss dialogs
Javascript pop ups such as alerts, prompts and confirmations cause rendering of the site to stop, which leads to an empty screenshot. Calling dismissDialogs()
method automatically closes such popups allowing the screenshot to be taken.
Disable Javascript
If you want to completely disable javascript when capturing the page, use the disableJavascript()
method.
Be aware that some sites will not render correctly without javascript.
Waiting for lazy-loaded resources
Some websites lazy-load additional resources via ajax or use webfonts, which might not be loaded in time for the screenshot. Using the waitUntilNetworkIdle()
method you can tell Browsershot to wait for a period of 500 ms with no network activity before taking the screenshot, ensuring all additional resources are loaded.
Alternatively you can use less strict waitUntilNetworkIdle(false)
, which allows 2 network connections in the 500 ms waiting period, useful for websites with scripts periodically pinging an ajax endpoint.
Delayed screenshots
You can delay the taking of screenshot by setDelay()
. This is useful if you need to wait for completion of javascript or if you are attempting to capture lazy-loaded resources.
Waiting for javascript function
You can also wait for a javascript function until is returns true by using waitForFunction()
. This is useful if you need to wait for task on javascript which is not related to network status.
Adding JS
You can add javascript prior to your screenshot or output using the syntax for Puppeteer's addScriptTag.
Adding CSS
You can add CSS styles prior to your screenshot or output using the syntax for Puppeteer's addStyleTag.
Output directly to the browser
You can output the image directly to the browser using the screenshot()
method.
PDFs
Browsershot will save a pdf if the path passed to the save
method has a pdf
extension.
Alternatively you can explicitly use the savePdf
method:
You can also pass some html which will be converted to a pdf.
Sizing the pdf
You can specify the width and the height.
Optionally you can give a custom unit to the paperSize
as the third parameter.
Setting margins
Margins can be set.
Optionally you can give a custom unit to the margins
as the fifth parameter.
Headers and footers
By default a PDF will not show the header and a footer generated by Chrome. Here's how you can make the header and footer appear. You can also provide a custom HTML template for the header and footer.
In the header and footer HTML, any tags with the following classes will have its printing value injected into its contents.
date
formatted print datetitle
document titleurl
document locationpageNumber
current page numbertotalPages
total pages in the document
To hide the header or footer, you can call either hideHeader
or hideFooter
.
Backgrounds
By default, the resulting PDF will not show the background of the html page. If you do want the background to be included you can call showBackground
:
Landscape orientation
Call landscape
if you want to resulting pdf to be landscape oriented.
Only export specific pages
You can control which pages should be export by passing a print range to the pages
method. Here are some examples of valid print ranges: 1
, 1-3
, 1-5, 8, 11-13
.
Output directly to the browser
You can output the PDF directly to the browser using the pdf()
method.
HTML
Browsershot also can get the body of an html page after JavaScript has been executed:
Evaluate
Browsershot can get the evaluation of an html page:
Misc
Setting an arbitrary option
You can set any arbitrary options by calling setOption
:
Fixing cors issues
If you experience issues related to cors, you can opt to disable cors checks with --disable-web-security.
Changing the language of the browser
You can use setOption
to change the language of the browser.
In order to load a page in a specific language for example.
Setting the user agent
If you want to set the user agent Google Chrome should use when taking the screenshot you can do so:
Setting the CSS media type of the page
You can emulate the media type, especially usefull when you're generating pdf shots, because it will try to emulate the print version of the page by default.
The default timeout of Browsershot is set to 60 seconds. Of course, you can modify this timeout:
Disable sandboxing
When running Linux in certain virtualization enviroments it might need to disable sandboxing.
Ignore HTTPS errors
You can ignore HTTPS errors, if necessary.
Specify a proxy Server
You can specify a proxy server to use when connecting. The argument passed to setProxyServer
will be passed to the --proxy-server=
option of Chromium. More info here: https://www.chromium.org/developers/design-documents/network-settings#TOC-Command-line-options-for-proxy-settings
Setting extraHTTPHeaders
To send custom HTTP headers, set the extraHTTPHeaders option like so:
Using HTTP Authentication
You can provide credentials for HTTP authentication:
Using Cookies
You can add cookies to the request to the given url:
You can specify the domain to register cookies to, if necessary:
Clicking on the page
You can specify clicks on the page.
Typing on the page
You can type on the page (you can use this to fill form fields).
You can combine type
and click
to create a screenshot of a page after submitting a form:
Changing the value of a dropdown value
You can change the value of a dropdown on the page (you can use this to change form select fields).
You can combine selectOption
, type
and click
to create a screenshot of a page after submitting a form:
Writing options to file
When the amount of options given to puppeteer becomes too big, Browsershot will fail because of an overflow of characters in the command line. Browsershot can write the options to a file and pass that file to puppeteer and so bypass the character overflow.
Related packages
- Laravel wrapper: laravel-browsershot
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] 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 lookat at v1
of this package which uses the abandoned PhantomJS
binary.
Postcardware
You're free to use this package (it's MIT-licensed), but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.
All postcards are published on our website.
Credits
- Freek Van der Herten
- All Contributors
Support us
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.
License
The MIT License (MIT). Please see License File for more information.
All versions of browsershot with dependencies
spatie/image Version ^1.5.3
spatie/temporary-directory Version ^1.1
symfony/process Version ^4.2