Examples
How to convert HTML to a base64 image with Puppeteer and PHP
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.
Get the base64 encoded image of some HTML and print to screen
<?php
require __DIR__ . '/vendor/autoload.php';
use HeadlessChromium\BrowserFactory;
$browser = (new BrowserFactory())->createBrowser();
$page = $browser->createPage();
$page->setHtml('<html><body><h1>Hello World</h1></body></html>');
echo $page->screenshot()->getBase64();
$browser->close(); Find out how to generate a PNG from HTML with PHP and Puppeteer.
That was 13 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.