Examples

HTML to PNG with PHP and Selenium

Install php-webdriver and chromedriver

composer require php-webdriver/webdriver
chromedriver --port=9515

php-webdriver has no Selenium Manager, so it needs a running chromedriver matching your Chrome version. Start it in another terminal and leave it running while the script runs — the examples connect to port 9515, chromedriver’s default, and passing --port=9515 just makes that explicit.

Convert HTML to PNG and save to file

<?php
require __DIR__ . '/vendor/autoload.php';

use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;

$options = new ChromeOptions();
$options->addArguments(['--headless=new']);

$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);

$driver = RemoteWebDriver::create('http://localhost:9515', $capabilities);

$html = '<html><body><h1>Hello World</h1></body></html>';
$driver->get('data:text/html;charset=utf-8,' . rawurlencode($html));

$driver->takeScreenshot('html.png');

$driver->quit();

Find out how to change the size of the viewport with PHP and Selenium.

That was 21 lines of code — plus a browser to run in production

Keeping headless browsers fed, patched and scaled is a job in itself. We built Urlbox so the same screenshot is one API call:

curl "https://api.urlbox.com/v1/YOUR_API_KEY/png?url=https://example.com"

Try the Urlbox screenshot API — free trial, no browser babysitting.