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.
Recommended Free Tools
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
- 14" diagonal, 1366x768 resolution, HD BrightView LED, Glossy NON-TOUCH Display
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:
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11$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
- 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.
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.
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →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. |
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
- 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, andcapture_pdftools 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.
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.
Quick Recap
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.




