Examples

Screenshot of a particular quality with PHP and Puppeteer

Heads up before you start

Puppeteer has no official PHP port. The closest equivalent is chrome-php — an actively maintained PHP library that controls headless Chrome over the same DevTools Protocol Puppeteer uses. These examples use chrome-php.

Install chrome-php

composer require chrome-php/chrome

chrome-php drives a Chrome or Chromium you already have installed — it does not download its own browser. It uses the CHROME_PATH environment variable if set, and otherwise looks in the standard install locations for your OS.

Take a screenshot with the quality level set and save to file

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

use HeadlessChromium\BrowserFactory;

$browser = (new BrowserFactory())->createBrowser();

$page = $browser->createPage();
$page->navigate('https://www.htmltoimage.com/demo')->waitForNavigation();

$page->screenshot([
    'format' => 'jpeg',
    'quality' => 75,
])->saveToFile('quality.jpg');

$browser->close();

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

That was 16 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.