PHP code example of stomaskov / browsershot
1. Go to this page and download the library: Download stomaskov/browsershot library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
stomaskov / browsershot example snippets
use Spatie\Browsershot\Browsershot;
// an image will be saved
Browsershot::url('https://example.com')->save($pathToImage);
// a pdf will be saved
Browsershot::url('https://example.com')->save('example.pdf');
Browsershot::html('<h1>Hello world!!</h1>')->save('example.pdf');
Browsershot::url('https://example.com')->bodyHtml(); // returns the html of the body
$requests = Browsershot::url('https://example.com')
->triggeredRequests();
foreach ($requests as $request) {
$url = $request['url']; //https://example.com/
}
Browsershot::html('Foo')
->setIncludePath('$PATH:/usr/local/bin')
Browsershot::html('Foo')
->setNodeModulePath("/path/to/my/project/node_modules/")
Browsershot::html('Foo')
->setBinPath("/path/to/my/project/my_script.js")
Browsershot::html('Foo')
->setChromePath("/path/to/my/chrome")
Browsershot::html('Foo')
->addChromiumArguments([
'some-argument-without-a-value',
'keyed-argument' => 'argument-value',
]);
Browsershot::html('Foo')
->addChromiumArguments([
'font-render-hinting' => 'none',
]);
use Spatie\Browsershot\Browsershot;
Browsershot::url('https://example.com')->save($pathToImage);
Browsershot::url('https://example.com')
->setScreenshotType('jpeg', 100)
->save($pathToImage);
Browsershot::url('https://example.com')
->windowSize(640, 480)
->save($pathToImage);
Browsershot::url('https://example.com')
->windowSize(1920, 1080)
->fit(Manipulations::FIT_CONTAIN, 200, 200)
->save($pathToImage);
Browsershot::url('https://example.com')
->clip($x, $y, $width, $height)
->save($pathToImage);
Browsershot::url('https://example.com')
->select('.some-selector')
->save($pathToImage);
$base64Data = Browsershot::url('https://example.com')
->base64Screenshot();
Browsershot::url('https://example.com')
->windowSize(640, 480)
->greyscale()
->save($pathToImage);
Browsershot::url('https://example.com')
->fullPage()
->save($pathToImage);
Browsershot::url('https://example.com')
->deviceScaleFactor(2)
->save($pathToImage);
Browsershot::url('https://example.com')
->userAgent('My Mobile Browser 1.0')
->mobile()
->touch()
->save($pathToImage);
$browsershot = new Browsershot('https://example.com', true);
$browsershot
->device('iPhone X')
->save($pathToImage);
Browsershot::url('https://example.com')
->userAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1')
->windowSize(375, 812)
->deviceScaleFactor(3)
->mobile()
->touch()
->landscape(false)
->save($pathToImage);
Browsershot::url('https://example.com')
->hideBackground()
->save($pathToImage);
Browsershot::url('https://example.com')
->dismissDialogs()
->save($pathToImage);
Browsershot::url('https://example.com')
->disableJavascript()
->save($pathToImage);
Browsershot::url('https://example.com')
->disableImages()
->save($pathToImage);
$urlsList = array("example.com/cm-notify?pi=outbrain", "sync.outbrain.com/cookie-sync?p=bidswitch");
Browsershot::url('https://example.com')
->blockUrls($urlsList)
->save($pathToImage);
$domainsList = array("googletagmanager.com", "googlesyndication.com", "doubleclick.net", "google-analytics.com");
Browsershot::url('https://example.com')
->blockDomains($domainsList)
->save($pathToImage);
Browsershot::url('https://example.com')
->waitUntilNetworkIdle()
->save($pathToImage);
Browsershot::url('https://example.com')
->setDelay($delayInMilliseconds)
->save($pathToImage);
Browsershot::url('https://example.com')
->waitForFunction('window.innerWidth < 100', $pollingInMilliseconds, $timeoutInMilliseconds)
->save($pathToImage);
Browsershot::url('https://example.com')
->setOption('addScriptTag', json_encode(['content' => 'alert("Hello World")']))
->save($pathToImage);
Browsershot::url('https://example.com')
->setOption('addStyleTag', json_encode(['content' => 'body{ font-size: 14px; }']))
->save($pathToImage);
$image = Browsershot::url('https://example.com')
->screenshot()
// a pdf will be saved
Browsershot::url('https://example.com')->save('example.pdf');
Browsershot::url('https://example.com')->savePdf('example.pdf');
Browsershot::html($someHtml)->savePdf('example.pdf');
Browsershot::html($someHtml)
->paperSize($width, $height)
->save('example.pdf');
Browsershot::html('https://example.com')->format('A4')->save('example.pdf');
Letter: 8.5in x 11in
Legal: 8.5in x 14in
Tabloid: 11in x 17in
Ledger: 17in x 11in
A0: 33.1in x 46.8in
A1: 23.4in x 33.1in
A2: 16.54in x 23.4in
A3: 11.7in x 16.54in
A4: 8.27in x 11.7in
A5: 5.83in x 8.27in
A6: 4.13in x 5.83in
Browsershot::html($someHtml)
->margins($top, $right, $bottom, $left)
->save('example.pdf');
Browsershot::html($someHtml)
->showBrowserHeaderAndFooter()
->headerHtml($someHtml)
->footerHtml($someHtml)
->save('example.pdf');
Browsershot::html($someHtml)
->showBackground()
->save('example.pdf');
Browsershot::html($someHtml)
->landscape()
->save('example.pdf');
Browsershot::html($someHtml)
->pages('1-5, 8, 11-13')
->save('example.pdf');
$pdf = Browsershot::url('https://example.com')
->pdf()
Browsershot::url('https://example.com')->bodyHtml(); // returns the html of the body
Browsershot::url('https://example.com')
->deviceScaleFactor(2)
->evaluate("window.devicePixelRatio"); // returns 2
Browsershot::url('https://example.com')
->setOption('landscape', true)
->save($pathToImage);
Browsershot::url('https://example.com')
->setOption('args', ['--disable-web-security'])
->save($pathToImage);
Browsershot::url('https://example.com')
->setOption('args', '--lang=en-GB')
...
Browsershot::url('https://example.com')
->userAgent('My Special Snowflake Browser 1.0')
->save($pathToImage);
Browsershot::url('https://example.com')
->emulateMedia('screen') // "screen", "print" (default) or null (passing null disables the emulation).
->savePdf($pathToPdf);
Browsershot::url('https://example.com')
->timeout(120)
->save($pathToImage);
Browsershot::url('https://example.com')
->noSandbox()
...
Browsershot::url('https://example.com')
->ignoreHttpsErrors()
...
Browsershot::url('https://example.com')
->setProxyServer("1.2.3.4:8080")
...
Browsershot::url('https://example.com')
->setExtraHttpHeaders(['Custom-Header-Name' => 'Custom-Header-Value'])
...
Browsershot::url('https://example.com')
->authenticate('username', 'password')
...
Browsershot::url('https://example.com')
->useCookies(['Cookie-Key' => 'Cookie-Value'])
...
Browsershot::url('https://example.com')
->useCookies(['Cookie-Key' => 'Cookie-Value'], 'ui.example.com')
...
Browsershot::url('https://example.com')
->click('#selector1')
// Right click 5 times on #selector2, each click lasting 200 milliseconds.
->click('#selector2', 'right', 5, 200)
Browsershot::url('https://example.com')
->type('#selector1', 'Hello, is it me you are looking for?')
Browsershot::url('https://example.com')
->type('#firstName', 'My name')
->click('#submit')
->delay($millisecondsToWait)
->save($pathToImage);
Browsershot::url('https://example.com')
->selectOption('#selector1', '100')
Browsershot::url('https://example.com')
->type('#firstName', 'My name')
->selectOption('#state', 'MT')
->click('#submit')
->delay($millisecondsToWait)
->save($pathToImage);
Browsershot::url('https://example.com')
->writeOptionsToFile()
...
Browsershot::url('https://example.com')
->setRemoteInstance('1.2.3.4', 9222)
...
Browsershot::url('https://example.com')
->usePipe()
...
Browsershot::url('https://example.com')
->setEnvironmentOptions(['TZ' => 'Pacific/Auckland'])
...
bash
npm install puppeteer --global