Automate Chrome with PHP: Install and Use ChromePHP for Screenshots, PDFs, and More
ChromePHP is a PHP library that integrates the Chrome DevTools protocol, allowing developers to control Chrome or Chromium directly from PHP code for tasks such as navigation, JavaScript execution, screenshots, PDF generation, and input simulation, with simple Composer installation and flexible configuration.
Overview
ChromePHP is a Chrome extension that embeds a PHP interpreter into the browser, enabling developers to write and run PHP code without leaving the current page, which streamlines development workflows and provides convenient debugging tools.
Features
Open Chrome or Chromium from PHP
Create pages and navigate to URLs
Capture screenshots
Execute JavaScript on the page
Generate PDFs
Simulate mouse actions
Simulate keyboard input
IDE‑friendly throughout
Requirements
PHP 7.4 – 8.5
Chrome/Chromium 65+ executable
Tested on Linux; works on macOS and Windows
Installation
Install via Composer:
composer require chrome-php/chromeUsage Example
use HeadlessChromium\BrowserFactory;
$browserFactory = new BrowserFactory();
$browser = $browserFactory->createBrowser();
try {
$page = $browser->createPage();
$page->navigate('http://example.com')->waitForNavigation();
// Get page title
$pageTitle = $page->evaluate('document.title')->getReturnValue();
// Save screenshot
$page->screenshot()->saveToFile('/foo/bar.png');
// Save PDF
$page->pdf(['printBackground' => false])->saveToFile('/foo/bar.pdf');
} finally {
$browser->close();
}Using a Different Chrome Executable
$browserFactory = new BrowserFactory('chromium-browser'); // specify path or nameDebug Mode (Non‑Headless)
$browser = $browserFactory->createBrowser(['headless' => false]); // show browser windowAdditional Feature Highlights
Custom browser options (window size, no‑image mode, user‑agent, proxy, etc.)
Persistent browser connections (keepAlive)
Page operations: navigation, JS execution, script injection, viewport setting, full‑page or cropped screenshots, PDF generation with custom margins, headers/footers
Mouse and keyboard simulation
Cookie management
DOM manipulation (query elements, click, type text, etc.)
File download support
This library is well suited for automation tasks such as web page screenshots, PDF creation, web crawling, and testing, leveraging Chrome's DevTools protocol to provide powerful capabilities.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
