October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
Browsershot

PHP HTML to Image: Render HTML with Chromium or an API

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To convert HTML to an image in PHP, render it in a browser engine and capture the result. For a local workflow, use a PHP library that controls headless Chrome or Chromium, such as Browsershot or chrome-php/chrome. If you do not want to install and operate a browser, send HTML and CSS to a hosted rendering API instead. Choose a PDF library only when your deliverable is a PDF—not a raster screenshot.

Choose the rendering path that fits your project

HTML-to-image conversion is not simply a matter of saving markup as a PNG. CSS layout, fonts, images, and JavaScript need to be rendered before pixels can be captured. A headless browser does that work and can produce a screenshot of a URL, an HTML document, or a selected region.

Approach Where it renders Useful when Main trade-off
Browsershot Chrome controlled through Puppeteer You want a PHP-facing interface for rendering a URL, HTML string, or local file Your deployment must include and maintain the browser tooling and its requirements. Browsershot README
chrome-php/chrome Chrome or Chromium controlled directly from PHP You need browser navigation, JavaScript evaluation, or screenshot controls from PHP Chrome/Chromium and a compatible PHP runtime are required. The project lists PHP 7.4–8.5 and Chrome/Chromium 65+; check its current requirements for your deployment. Project documentation
Hosted HTML/CSS rendering API A vendor-operated Google Chrome service You prefer HTTP requests over running a browser on your own server Requires credentials and sending the HTML/CSS and any referenced assets to an external service. PHP integration documentation
Dompdf or mPDF PDF rendering workflow Your intended output is a PDF document These are not established here as drop-in browser screenshot engines for raster images. Dompdf documentation and mPDF manual

For JavaScript-driven pages or browser-like CSS behavior, use a browser-based method. For a fixed, known document intended for printing or download, a PDF library may be more appropriate. For a server that cannot host Chrome, consider an API, after reviewing its data-handling terms.

Render HTML locally with PHP and headless Chrome

Browsershot and chrome-php/chrome both use Chrome-family browsers, but their PHP interfaces differ. Browsershot delegates browser control to Puppeteer; chrome-php/chrome exposes browser operations directly in PHP. In either case, the PHP package alone is not the whole runtime: install the required browser components and confirm they work in the same environment that runs your application, such as a container or worker.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Browsershot: render a URL, HTML string, or file

The Browsershot README documents rendering a URL, an HTML string, or a local HTML file and writing an image or PDF to a destination. Install the package using its current project instructions, then adapt this minimal example to the method signatures and Puppeteer requirements in the version you install:

<?php

use SpatieBrowsershotBrowsershot;

Browsershot::url('https://example.com')
    ->save('/tmp/page.png');

For HTML you generate in PHP, the corresponding interface is an HTML source rather than a URL:

<?php

use SpatieBrowsershotBrowsershot;

$html = '<!doctype html><html><body><h1>Monthly report</h1></body></html>';

Browsershot::html($html)
    ->save('/tmp/report.png');

These examples show the basic workflow; consult the Browsershot README and the installed version’s documentation for setup and capture options. If rendering a file, ensure its images, stylesheets, and fonts resolve correctly from the browser’s environment.

chrome-php/chrome: control a browser page explicitly

The chrome-php/chrome project documents opening a browser, creating a page, navigating to a URL, setting page HTML, evaluating JavaScript, and taking screenshots. A URL capture follows this general sequence; use the repository’s current API examples for exact constructor and screenshot options in your installed release:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<?php

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

use HeadlessChromiumBrowserFactory;

$browser = (new BrowserFactory())->createBrowser();
try {
    $page = $browser->createPage();
    $page->navigate('https://example.com')->waitForNavigation();
    $page->screenshot()->saveToFile('/tmp/page.png');
} finally {
    $browser->close();
}

The package documents PNG, JPEG, and WebP screenshots, including viewport and clipped captures. Its repository lists PHP 7.4–8.5 and Chrome/Chromium 65+ as requirements, but those broad ranges are not a guarantee that every combination works with your current deployment. Verify the package’s current compatibility notes and test the browser binary where the PHP process actually runs. See chrome-php/chrome documentation.

Set capture dimensions and wait for the page to be ready

A screenshot captures the rendered state at a particular moment and size. Decide whether the image should show a fixed viewport, the whole page, or a clipped element. A viewport screenshot is useful for a predictable card or social graphic; a full-page capture suits long content; a clipped screenshot targets a particular region when the library supports it.

  • Set the viewport deliberately. Responsive layouts change with width, and text wrapping changes image height. Use the same dimensions in development and production.
  • Wait for rendering. A navigation event may finish before asynchronous content, web fonts, or lazy images are ready. Wait for a page-specific selector or other reliable readiness condition if the library supports it.
  • Use stable assets. Local files and remote images must be accessible to the browser process. Check font and image loading rather than assuming that successful HTML navigation means the assets appeared.
  • Keep the page deterministic. Dynamic timestamps, rotating content, animations, and external requests can produce different output across runs. Disable or control these where repeatability matters.

The libraries differ in their exposed controls, so use the documentation for the installed version rather than assuming a method or option exists in both.

Use a hosted API instead of operating Chrome

A hosted renderer can simplify server setup: PHP makes an HTTP request and the service performs the browser rendering. HTML/CSS to Image documents a PHP cURL example that submits markup and styles, renders them in Google Chrome, and returns JSON containing a generated image URL. Its PHP page also lists PNG, JPG, WebP, and PDF outputs and shows Guzzle as an alternative HTTP client. Review the current instructions at HTML/CSS to Image’s PHP documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Before sending a page to any hosted renderer, check whether the HTML, CSS, image URLs, fonts, cookies, or other data contain confidential information. A hosted request changes where rendering happens and may mean content is transmitted outside your infrastructure. Also verify current pricing, request limits, concurrency, retention, and service terms with the vendor; those details are not established by the integration example.

Or skip the browser setup: ScreenshotNeo

If the task is capturing a website URL rather than rendering custom markup locally, ScreenshotNeo offers a one-request screenshot API and an MCP server for AI agents. Cookie banners, newsletter popups, and chat widgets are removed before capture; bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, with response headers indicating the page verdict and billing status. Its MCP tools include take_screenshot, get_page_info, and capture_pdf.

For a PHP application, make a GET request and save the returned image bytes:

<?php

import requests

$r = requests.get(
    "https://api.screenshotneo.com/v1/shot",
    params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"},
    timeout=90,
)
open("shot.webp", "wb").write(r.content)

The code above is Python, not PHP. PHP can call the same endpoint with cURL:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<?php

$ch = curl_init('https://api.screenshotneo.com/v1/shot?access_key=' . rawurlencode('YOUR_API_KEY') . '&url=' . rawurlencode('https://stripe.com'));
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 90,
]);
$image = curl_exec($ch);
if ($image === false) {
    throw new RuntimeException(curl_error($ch));
}
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status < 200 || $status >= 300) {
    throw new RuntimeException('Screenshot request returned HTTP ' . $status);
}
file_put_contents('shot.webp', $image);

Alternatively, the documented cURL, Python, and Node.js examples are available alongside request parameters in the ScreenshotNeo documentation. The API supports PNG, JPEG, WebP, or PDF and can also accept HTML/CSS, capture a CSS-selected element, wait for a selector or delay, set viewport and device options, and more. Plans include 1,000 screenshots per month free with no card; paid plans start at $5 for 3,000 shots. See all plans and sign up free for 1,000 screenshots a month with no card.

Choose an image format and output workflow

PNG, JPEG/JPG, and WebP are raster image formats. PNG is commonly useful for crisp interface graphics and transparency; JPEG is suitable when a smaller photographic image matters more than lossless edges; WebP can be useful where the consuming platform supports it. Confirm that the target social network, CMS, or downstream tool accepts the format you produce. The chrome-php/chrome documentation lists PNG, JPEG, and WebP; HTML/CSS to Image lists PNG, JPG, WebP, and PDF.

For a PDF, use a PDF-capable renderer or a screenshot API that explicitly supports PDF output. Dompdf describes a mostly CSS 2.1 layout engine with some CSS3 support and controls for local and remote resources; mPDF documents writing HTML into a PDF. Neither cited source establishes these libraries as direct raster-image screenshot tools. See Dompdf and the mPDF manual.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Security, performance, and operating cost

Protect your application and the pages it renders

Rendering arbitrary URLs can expose a server-side browser to untrusted pages and network destinations. Restrict which URLs your application accepts, avoid passing secrets into pages that can be captured, and consider how the browser accesses local and remote resources. For PDF workflows that process externally supplied HTML or CSS, the mPDF manual specifically warns that the content should be vetted and sanitized above standard browser-level sanitization. Treat untrusted markup as input to secure, not as harmless text.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Plan for browser resources and concurrency

A local browser adds a runtime dependency beyond PHP. Browser startup, page assets, fonts, JavaScript, and the size of the capture all affect work performed per request. Reuse or queue browser work appropriately for your application, set timeouts, and avoid launching unbounded concurrent captures. Test memory and execution limits under representative pages; no universal resource figure is established by the package documentation.

Compare total cost, not just the request method

A local installation shifts cost to infrastructure, browser maintenance, and operational troubleshooting. A hosted API shifts some of that burden to a service dependency and may charge according to its plan or usage. The cited implementation documentation does not establish current pricing, request limits, concurrency, or retention for the third-party hosted renderer, so verify those terms before choosing it. With either route, measure the time and infrastructure needed for your own pages instead of assuming one method will always be cheaper or faster.

Troubleshoot common failures

  • Browser executable not found: The PHP package is installed but Chrome/Chromium or Puppeteer is missing, unavailable to the process, or installed outside its expected path. Install the browser prerequisites from the library’s current instructions and verify access as the same user that runs PHP.
  • Works locally but fails in production: The production container or server may lack browser dependencies, writable temporary storage, fonts, or network access to assets. Reproduce the capture inside the deployed environment and inspect its logs and browser output.
  • Image is blank or missing content: The browser may have captured before JavaScript, fonts, images, or delayed API content finished loading. Wait on a meaningful selector or readiness condition, and verify that remote assets are reachable from the browser.
  • Text or layout differs from the expected design: A changed viewport, missing font, responsive breakpoint, or different browser version can change wrapping and layout. Pin the viewport and fonts, and compare the actual browser environment against development.
  • Screenshot is cropped or unexpectedly tall: Confirm whether the capture is viewport-sized, full-page, or clipped. Set explicit dimensions and use the library’s documented full-page or clipping controls where available.
  • Hosted API returns an error or unusable result: Check credentials, request encoding, output format, and whether the HTML or referenced assets can be accessed by the service. Consult the vendor’s current error and limit documentation; do not assume an image URL means the output has already been downloaded to your server.
  • PHP times out: Rendering a large page or waiting on slow assets can exceed the request’s timeout. Set a deliberate timeout, reduce unnecessary content, and move long captures to a queue or asynchronous workflow when supported.

Practical decision

Use Browsershot or chrome-php/chrome when you need a browser-rendered image and can operate the browser runtime in your own environment. Use a hosted API when avoiding that runtime is more important and your privacy, reliability, and cost requirements allow external rendering. Use Dompdf or mPDF when the actual deliverable is a PDF. For a public website screenshot API, ScreenshotNeo is the first hosted option to try when clean captures and billing only for successful, non-cached shots matter; evaluate any hosted service against your own data and operational requirements.

Frequently Asked Questions

Can PHP convert HTML directly to a PNG without a browser?

The approaches documented here use Chrome/Chromium for browser-like HTML rendering. PDF-focused libraries are a different workflow and are not established here as direct raster screenshot tools.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Can I capture only one element instead of the full page?

Yes, browser libraries can support clipped captures, and ScreenshotNeo supports capture by CSS selector. Check the installed library’s current documentation for its exact option.

Is an HTML-to-PDF library the same as an HTML-to-image library?

No. Dompdf and mPDF target PDF documents; a screenshot renderer produces raster image output.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Read next

Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.