DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk5 min

Use Laravel Browsershot to Screenshot a Webpage with Custom CSS

Use Puppeteer’s addStyleTag option through Laravel Browsershot to inject CSS before saving a webpage screenshot or returning its image bytes.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To inject CSS before a Laravel Browsershot screenshot, pass Puppeteer’s addStyleTag option through setOption(), then call save() to write an image or screenshot() to get its bytes. The CSS is applied in the page before capture, so it can adjust the rendered appearance without changing the live site.

Inject CSS before capturing the page

For a live webpage, start with Browsershot::url(). The documented Browsershot v4 pattern passes a JSON-encoded option containing CSS text to Puppeteer’s addStyleTag:

As an Amazon Associate I earn from qualifying purchases.

<?php

use SpatieBrowsershotBrowsershot;

$url = 'https://example.com';
$pathToImage = storage_path('app/public/example.png');

Browsershot::url($url)
    ->setOption('addStyleTag', json_encode([
        'content' => 'body { font-size: 14px; }',
    ]))
    ->save($pathToImage);

Replace the sample URL, CSS, and output path with values appropriate to your application. This example follows the Browsershot v4 image guide; make sure the API matches the version installed in your project.

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

Use a stylesheet URL instead of inline CSS

Puppeteer’s addStyleTag accepts either CSS content or a stylesheet URL. To load a stylesheet, pass url instead of content:

#1 Best Overall
Browsershot::url('https://example.com')
    ->setOption('addStyleTag', json_encode([
        'url' => 'https://example.com/screenshot.css',
    ]))
    ->save($pathToImage);

The stylesheet URL must be reachable by the headless browser in the environment running the capture. See Puppeteer’s page.addStyleTag API for the accepted style-tag inputs.

Choose how to handle the screenshot output

Write an image file with save()

Use save($pathToImage) when the application should write the capture directly to a file. Choose a destination your PHP process can write to, and use the filename extension and output settings appropriate to your application.

Return image bytes with screenshot()

If the next step needs the image data rather than a file, call screenshot() and handle the returned bytes in your application:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
$imageBytes = Browsershot::url('https://example.com')
    ->setOption('addStyleTag', json_encode([
        'content' => 'body { font-size: 14px; }',
    ]))
    ->screenshot();

The image guide documents both methods. Choose based on whether your code needs a saved path or the returned image data.

Wait for the page state your capture needs

A page that renders or loads content asynchronously may not be ready at the instant the browser navigates to it. Browsershot’s image guide provides separate approaches for a delay, waiting for a JavaScript function, and waiting for a selector. Select the condition that corresponds to the content you need; a fixed delay is not a universal guarantee that the page is ready.

  • Use a delay when the page has a known, predictable render lag.
  • Wait for a JavaScript function when readiness is represented by a page-side condition.
  • Wait for a selector when a specific element must appear before capture.

Consult the matching sections of the image guide for the exact wait methods available to your installed Browsershot version.

Rank #2
Dell Latitude 5420 14" FHD Business Laptop Computer, Intel Quad-Core i5-1145G7, 16GB DDR4 RAM, 256GB SSD, Camera, HDMI, Windows 11 Pro (Renewed)
  • 256 GB SSD of storage.
  • Multitasking is easy with 16GB of RAM
  • Equipped with a blazing fast Core i5 2.00 GHz processor.

Choose the right Laravel capture tool

Browsershot uses Puppeteer and headless Chrome to render a URL or supplied HTML into an image or PDF. It is the directly documented choice here when you need a webpage image with injected CSS. Laravel Dusk’s screenshots serve a different purpose: browser-test evidence, including capturing a particular element. See the Browsershot image guide, Browsershot introduction, and Laravel Dusk screenshot documentation.

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.

If you use Spatie’s Laravel Screenshot wrapper rather than Browsershot directly, its withBrowsershot() method exposes the underlying instance for customization. However, closure-based customization is incompatible with saveQueued(), because closures cannot be serialized for queued saving. Check the Laravel Screenshot wrapper documentation before combining those features.

Deployment and security considerations

Match the installed dependencies and environment

Browsershot relies on Puppeteer and headless Chrome, so the required setup depends on the Browsershot, Puppeteer, Chrome, operating-system, and deployment versions in your project. Follow the compatibility and setup guidance for those versions rather than treating one installation command as universal.

In restricted environments such as some Docker configurations, Chrome may require no-sandbox mode. The Laravel Screenshot guide documents configuration globally or per screenshot. Enable it only where the environment requires it, and consult the guide for the applicable configuration.

Only capture trusted URLs and HTML

URLs and HTML passed to a headless browser are security-sensitive input. Spatie’s security documentation puts the responsibility on the user to validate them and advises passing only trusted URLs and HTML. Do not pass arbitrary user-controlled destinations into a screenshot job without validation.

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

Troubleshoot common capture problems

Symptom Likely cause What to check
The custom CSS has no visible effect The style option is malformed, the CSS does not match the rendered page, or the stylesheet URL cannot be reached by Chrome. Confirm the addStyleTag option is JSON-encoded with either content or url, inspect the selectors against the page’s actual markup, and verify stylesheet reachability from the capture environment.
The screenshot misses content that appears later The capture runs before client-side rendering or late content has completed. Use the wait approach that matches the page: a delay, a JavaScript function, or a selector. Avoid assuming one fixed wait suits every page.
The browser cannot render in the deployment environment Chrome or its runtime dependencies may be unavailable or restricted; some Docker environments also require no-sandbox mode. Check the installed package and Chrome setup for the deployed environment. Configure no-sandbox only if that environment needs it.
A queued wrapper capture fails with closure customization The Laravel Screenshot wrapper documentation says closures cannot be serialized for saveQueued(). Do not use closure-based withBrowsershot() customization with queued saving; use a compatible customization path for that workflow.
An unexpected destination or page is being captured Untrusted or insufficiently validated URL or HTML input is reaching the browser. Validate capture input and restrict it to trusted destinations and content before invoking Browsershot.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

ScreenshotNeo is a website screenshot API and MCP server. Send a GET request with a URL to receive an image or PDF, without installing Puppeteer and Chrome in your Laravel environment. The API parameter names used by other screenshot APIs also work, which can make switching easier.

Rank #3
15.6 Inch Win 11 Laptop Computer, N4020, 4GB DDR4 RAM, 128GB Storage | LPDDR4 RAM, 180° Lay-Flat, Dual-Band WiFi, Type-C PD Fast Charging, Thin & Lightweight for Students, Home & Office, Gray
  • WINDOWS 11 | STABLE PERFORMANCE: Powered by Intel Celeron N4020 processor and Windows 11 system, this laptop delivers stable performance for everyday computing tasks. It supports web browsing, online learning, document editing, email communication, and basic office work with optimized power efficiency, providing a practical and reliable experience for essential daily use for daily use.
  • 15.6” FHD IPS DISPLAY: Features a 15.6-inch Full HD IPS display with narrow bezels, offering wider viewing angles and clearer image details compared to standard panels. The improved screen-to-body ratio enhances visual experience for study, reading, document work, and video playback, making it suitable for both productivity and entertainment use.
  • 4GB DDR4 + 128GB eMMC STORAGE: Equipped with 4GB DDR4 memory and 128GB eMMC storage for everyday basics such as browsing, documents, email, and online learning platforms. The built-in TF card slot supports storage expansion up to 1TB, giving you more flexibility for files, photos, videos, and daily documents. TF card not included.
  • CONNECTIVITY & PORTS: Includes 1× TF card slot, 2× USB 3.2 Gen1 ports, and 2× full-featured Type-C ports (USB 3.2 Gen1). The Type-C ports support data transfer, charging, and video output, enabling flexible connection with external devices such as monitors, storage, and peripherals for daily work and study use.
  • LIGHTWEIGHT DESIGN | ONLINE COMMUNICATION: Designed with a slim, portable profile, this laptop is easy to carry for school, commuting, and travel. A built-in 1MP front camera supports online classes, video meetings, remote communication, and everyday conferencing. The 3300mAh battery works with the low-power system design to support practical daily use, while thermal optimization helps maintain quieter operation during extended tasks.

For a first capture, create an API key and try this cURL request. Replace the target URL if needed; see the ScreenshotNeo API documentation for available formats and options.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
  • Cookie and consent banners, newsletter popups, and chat widgets are removed before capture; each cleanup step can be turned off.
  • Bot checks and CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed. Response headers identify the page verdict and billing status.
  • An MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents and other MCP clients.
  • The Free plan includes 1,000 screenshots per month without a card; paid plans start at $5 for 3,000 screenshots. Yearly billing gives two months free, and every feature is available on every plan.

Sign up for ScreenshotNeo’s free plan to try 1,000 screenshots a month with no card.

Frequently Asked Questions

Can Browsershot inject CSS into a page without changing the website?

Yes. The CSS is added for the browser rendering used for the capture; it does not require editing the live site’s stylesheet.

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

Can I use this approach with HTML instead of a live URL?

Browsershot supports supplied HTML as well as a URL. Check the API for the Browsershot version installed in your project.

Does the code in this guide prove compatibility with every Laravel or Browsershot version?

No. The example follows the Browsershot v4 image guide. Verify the matching documentation and dependencies for your project’s installed versions.

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.