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
World desk6 min

Convert a URL to PDF in PHP with Headless Chrome

A practical guide to turning a live URL into a PDF with PHP and headless Chrome, including runnable Browsershot and chrome-php/chrome examples, CLI options, print controls, and troubleshooting.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To convert a live URL to PDF in PHP, use Spatie Browsershot for a concise PHP API, chrome-php/chrome for direct control of Chrome from PHP, or Chrome’s headless command line if invoking a browser executable suits your deployment. All three rely on Chrome rendering the page; choose based on how much browser lifecycle and print control your application needs.

Choose a PHP-to-PDF approach

Approach PHP integration Browser setup Print controls documented Best fit
Browsershot Concise URL-to-file API Puppeteer controls headless Chrome URL, HTML string, or local HTML file; consult current docs for detailed options A straightforward PHP-facing interface
chrome-php/chrome Direct browser and page API Chrome or Chromium controlled from PHP Paper dimensions, margins, scale, orientation, CSS page size, backgrounds, headers and footers, and streaming Fine-grained page or browser control
Chrome CLI PHP must launch the executable Installed Chrome invoked in headless mode Print to PDF, omit headers and footers, set a capture timeout, and use a virtual-time budget A command-line workflow when process management is suitable

Compatibility details move with package releases. The Browsershot version 5.4.0 listing checked on 2026-10-03 requires PHP ^8.2; its release date was 2026-05-26. The chrome-php/chrome README lists PHP 7.4–8.5 and Chrome/Chromium 65 or later. Confirm current requirements before installing: Packagist Browsershot package metadata, Browsershot README, and the Chrome PHP README.

As an Amazon Associate I earn from qualifying purchases.

Use Browsershot for a concise PHP API

Install the package with Composer, then pass the URL and a PDF filename to save():

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
composer require spatie/browsershot
<?php

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

use SpatieBrowsershotBrowsershot;

Browsershot::url('https://example.com')
    ->save(__DIR__ . '/example.pdf');

Browsershot renders through Puppeteer controlling headless Chrome; it is not a PHP-only renderer. The project README identifies Puppeteer as a test-suite requirement and points to separate requirements documentation. Check its current setup instructions for the production environment rather than assuming that a test command fully installs the runtime: Browsershot README.

The same package accepts an HTML string or a local HTML file as input. For URL conversion, use Browsershot::url() as shown; use the project documentation for the appropriate methods and options when the input is not a URL.

Use chrome-php/chrome for browser and print control

Install the library and create a browser, page, navigation, and PDF. Close the browser in a finally block so it is cleaned up if navigation or PDF generation throws an exception.

composer require chrome-php/chrome
<?php

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

use HeadlessChromiumBrowserFactory;

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

try {
    $page = $browser->createPage();
    $page->navigate('https://example.com')->waitForNavigation();
    $page->pdf(['printBackground' => true])
        ->saveToFile('/tmp/example.pdf');
} finally {
    $browser->close();
}

The documented PDF options include orientation, whether to print backgrounds, whether CSS @page sizing takes precedence, margins, paper dimensions, scale, and header and footer templates. Output can be saved to a file or streamed to a PHP output stream. Header and footer templates can use classes for the date, title, URL, page number, and total page count. Consult the library README for the current option names and accepted values: Chrome PHP README.

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

Set the Chrome executable and navigation wait

The library documents CHROME_PATH for selecting a Chrome executable, and also permits specifying an executable name in BrowserFactory. Its default navigation wait is for the load event; alternatives include DOM content loaded and network idle. A page that continues loading background resources or populates content after initial navigation may need a different event. Choose the wait condition to match the page, and handle timeouts in application code.

Use Chrome’s headless command line

When PHP can safely invoke an installed Chrome executable, Chrome’s CLI offers the shortest conversion command:

chrome --headless --print-to-pdf https://example.com/

Chrome saves output.pdf in the current working directory. Use --no-pdf-header-footer to omit Chrome’s generated date, URL, and page header and footer. The --timeout=5000 flag caps the wait at five seconds; it can also capture a slow page before it is ready. --virtual-time-budget is available to advance time-dependent page code during capture. See Chrome’s headless command-line reference (last updated 2024-10-21).

The command above is a shell example, not PHP process-launch code. If PHP starts Chrome, treat process execution as deployment-specific integration work: use safe argument handling, configure a process timeout, capture exit status and error output, and verify that the expected PDF was produced. Do not build a shell command by concatenating an untrusted URL.

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

Make the PDF match the page you need

  • Choose readiness deliberately. A load event, DOM content loaded, network idle, or a bounded CLI timeout can produce different results. A short fixed wait trades completeness for speed; background requests can also make waiting for network idle unsuitable.
  • Choose print layout explicitly. For chrome-php/chrome, configure paper size or dimensions, margins, orientation, scale, background printing, and CSS @page preference as needed. The library also supports headers and footers. These settings determine print output; they do not guarantee every website will paginate identically.
  • Account for dynamic pages. Pages that reveal content after a delay or depend on time-sensitive scripts may need a suitable wait condition or virtual-time budget. A timeout only bounds waiting; it does not prove that the page is complete.
  • Keep output paths intentional. Use a writable destination and make it clear whether the generated PDF is temporary or should be retained. The examples use a local file path; the PHP library also documents streaming output.

Troubleshoot common conversion failures

Symptom Likely cause What to check
Chrome cannot start The executable is absent, inaccessible, or not the one the library is using. Confirm Chrome or Chromium is installed and available to the PHP process. For chrome-php/chrome, check CHROME_PATH or the executable passed to BrowserFactory.
Composer refuses installation The PHP version does not meet the package’s current requirement. Check the live package metadata and your CLI/runtime PHP versions. Browsershot 5.4.0’s listing checked on 2026-10-03 specifies ^8.2; requirements may change.
PDF is blank or missing late content Navigation completed before the page’s scripts or delayed content were ready. Try a different documented navigation event for chrome-php/chrome, or use an appropriate CLI timeout or virtual-time budget. Longer waiting can increase latency and still cannot guarantee a site has finished updating.
Backgrounds or page sizing differ from expectations Print options or CSS print rules do not match the intended layout. Review printBackground, CSS @page preference, margins, paper size, orientation, and scale in the library’s current documentation.
CLI output is not where expected Chrome writes output.pdf to its current working directory. Run from the intended directory or account for that working directory in the PHP process integration.
PHP hangs while invoking Chrome The child process is waiting on a slow page or is not being managed with a suitable timeout. Set a process-level timeout, collect exit status and error output, and choose a bounded page wait appropriate to the site.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If you only need a URL-to-PDF API call, ScreenshotNeo returns a PDF from one GET request. For example, using cURL:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.pdf

See the ScreenshotNeo API documentation for request options and output details. ScreenshotNeo accepts cookie and consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each cleanup step can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and responses identify the page verdict and billing status in headers. It also offers an MCP server with screenshot, page-info, and PDF tools for AI agents. The free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots. Visit ScreenshotNeo or sign up free.

Operational considerations

With either PHP library or the CLI, your application is responsible for the browser runtime and its lifecycle. Browser startup, page readiness, and PDF rendering all contribute to request time; no performance benchmark is established here. For a web request handler, set timeouts and decide whether long renders belong in a background job. For repeatable output, record the browser and package versions in your deployment and recheck package requirements when upgrading.

Rendering also depends on the target website’s current content, scripts, fonts, and print styles. Treat the produced PDF as a browser print result, not a guarantee that every live page will retain identical layout across environments.

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.

Frequently Asked Questions

Can I convert HTML instead of a URL with Browsershot?

Yes. Browsershot accepts an HTML string or a local HTML file as well as a URL; consult its README for the corresponding input methods.

Can chrome-php/chrome send the PDF directly to a response?

Its documentation says PDF output can be streamed to a PHP output stream as well as saved to a file.

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.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

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.