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.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Automate Chrome with PHP: Install and Use ChromePHP for Screenshots, PDFs, and More

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

Usage 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 name

Debug Mode (Non‑Headless)

$browser = $browserFactory->createBrowser(['headless' => false]); // show browser window

Additional 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPDevToolsWeb Scrapingpdf-generationheadless browserChrome automationChromePHP
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.