PHP code example of info2soft / browsershot

1. Go to this page and download the library: Download info2soft/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/ */

    

info2soft / 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

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);

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')
    ->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($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()
   ...
bash
npm install puppeteer --global