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

Use a headless browser such as Puppeteer or Playwright on your server. The script launches a browser, opens a page, navigates to the URL, waits for the content you need, saves the screenshot bytes, and closes the browser. An ordinary HTTP request can fetch a webpage’s HTML, but it does not render that page into pixels.

What a server-side screenshot script does

A webpage screenshot is an image of a browser-rendered page, not a copy of its HTML. Your server-side code therefore needs a browser engine. Puppeteer and Playwright both provide APIs to control a browser, set the page viewport, wait for the page, and capture an image.

The basic lifecycle is: launch the browser, create a page or context, navigate to the target, wait for the right readiness condition, capture the viewport, a full page, or an element, save the image, and close the browser. In a production service, make sure cleanup happens even if navigation or capture throws an error.

Generate a screenshot with Node.js and Puppeteer

This runnable ES-module example captures the full scrollable page at a fixed desktop viewport and writes a PNG to the current directory. It assumes Node.js and Puppeteer are installed in your project.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Install Puppeteer with npm install puppeteer.
  2. Save the following as screenshot.mjs.
  3. Run node screenshot.mjs. The result is screenshot.png.
import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();
try {
  const page = await browser.newPage();
  await page.setViewport({ width: 1280, height: 800, deviceScaleFactor: 1 });
  await page.goto('https://example.com', { waitUntil: 'networkidle2' });
  await page.screenshot({ path: 'screenshot.png', fullPage: true });
} finally {
  await browser.close();
}

The try/finally block ensures the browser is closed after either success or failure. Without cleanup, a failed navigation can leave a browser process running. The Puppeteer Page API documents the navigation and page lifecycle; see its screenshot API for capture behavior and options.

Capture only the visible viewport

By default, a screenshot covers the current viewport. Remove fullPage: true to capture just the 1280-by-800 viewport configured in the example. The image’s pixel dimensions can also be influenced by deviceScaleFactor.

Capture one element

If you need a card, chart, or other component rather than the whole page, wait for it and use the element’s screenshot method:

const card = await page.waitForSelector('.report-card', { timeout: 15000 });
if (!card) throw new Error('Report card was not found');
await card.screenshot({ path: 'report-card.png' });

Use a selector that uniquely identifies the component. A missing or late-rendering element should fail visibly rather than quietly producing an unrelated page image. Puppeteer documents element screenshots in its screenshot guide.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

Choose when the page is ready

The right wait condition depends on the page. waitUntil: 'networkidle2' can suit a page whose resources finish loading, but network quiet is not a universal signal that the content you care about is ready. Long polling, streaming, or analytics connections may keep network activity open, while client-side rendering can still be working after a navigation event.

  • Use a selector wait when a specific component is required: await page.waitForSelector('.report-card', { timeout: 15000 });
  • Use an application-specific readiness flag when your own page exposes one, rather than assuming all network activity will stop.
  • Bound navigation and selector waits with timeouts so a stalled page does not hold a worker forever.

Choose a signal that corresponds to what the screenshot must contain. If you capture too early, images or client-rendered elements may be missing; if you wait for universal network idleness on a continuously connected page, the capture may never happen.

Screenshot options that matter

Need Puppeteer approach Notes
Viewport image page.screenshot({ path: 'view.png' }) Captures the visible viewport.
Full document page.screenshot({ path: 'full.png', fullPage: true }) Captures the full scrollable page.
One component elementHandle.screenshot({ path: 'element.png' }) Locate and wait for the desired element first.
Clip a region clip option Specify a rectangular capture area in the screenshot options.
Choose image output type and, where supported, quality Puppeteer documents output type and quality controls; quality applies to lossy formats.
Transparent background omitBackground Useful when the page background should not be included.

For API details and supported options, consult Puppeteer’s screenshot options reference. Playwright also documents PNG, JPEG, and WebP output, clipping, masking, scale, and full-page capture in its screenshot documentation.

Using Playwright instead

Playwright follows the same broad sequence: launch a browser, create a context and page, navigate, wait, capture, and close. Its screenshot APIs support viewport, element, and full-page captures. A minimal example using Chromium is:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import { chromium } from 'playwright';

const browser = await chromium.launch();
try {
  const page = await browser.newPage({ viewport: { width: 1280, height: 800 } });
  await page.goto('https://example.com', { waitUntil: 'networkidle' });
  await page.screenshot({ path: 'screenshot.png', fullPage: true });
} finally {
  await browser.close();
}

Install the package and its browser binaries for your environment before running the script. Playwright’s screenshot guide describes capture targets and options. Choose between Puppeteer and Playwright based on your existing runtime, browser requirements, locator and wait workflow, deployment footprint, and CI setup. The cited documentation establishes their screenshot capabilities, not a universal performance or total-cost winner.

Make results more reproducible

The same URL can render differently when the viewport, browser version, operating system, hardware conditions, or headless mode changes. For screenshot comparisons or pixel-sensitive jobs, keep those conditions consistent and set the viewport and device scale explicitly. Playwright’s visual comparisons guidance also warns that rendering can vary with these environmental factors.

  • Pin and control the browser version used by your job.
  • Keep the operating system and headless configuration consistent between runs.
  • Set width, height, and device scale rather than relying on defaults.
  • Wait for a meaningful page signal before capturing.

Run screenshots as a server-side job

A one-off script can save directly to disk. For a URL-to-image endpoint or background worker, treat each capture as an isolated job: use a page or context for that job, enforce navigation and selector timeouts, capture the returned image data, and persist it to storage that survives worker shutdown if the worker is ephemeral. These are operational recommendations based on the browser/page lifecycle; the cited documentation does not prescribe a particular storage service or timeout value.

Concurrency requires care. Avoid sharing mutable page state across unrelated jobs; isolate work in separate pages or contexts and close them when finished. Also account for browser launch and page load time when deciding how much work one worker can safely process. No single benchmark or concurrency limit applies to every target site and deployment environment.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Troubleshooting common failures

The browser will not launch

Confirm that the package and its browser binary are installed for the server environment, and that the deployment can run the browser process. Local development and a minimal server image may not have the same dependencies. Check the launch error before changing capture logic.

Navigation times out

The page may be slow, stuck, or never reach the selected lifecycle condition. Use a bounded navigation wait and choose a readiness signal that fits the site. On pages with long polling or streaming, a selector or application-specific flag is often more appropriate than waiting for all network activity to settle.

The screenshot is blank or missing content

Check that navigation reached the intended URL and that the script waits for the element or state that should appear in the image. A successful navigation event is not proof that a client-rendered component has finished rendering. Wait for a required selector and inspect the page when the selector times out.

The image is cut off or too large

Use fullPage: true when the whole scrollable document is required. Use an element screenshot or a clip when only part of the page belongs in the output. Confirm that the requested output dimensions and page length are appropriate for the consuming system.

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.

Repeated captures do not match pixel for pixel

Standardize the browser version, operating system, viewport, device scale, hardware conditions, and headless mode. Then verify that dynamic page content is stable before capture; fixed browser settings cannot make a changing page deterministic.

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 want a hosted screenshot API rather than operating a browser, ScreenshotNeo accepts one GET request with a URL and returns a PNG, JPEG, WebP, or PDF. For example, using cURL:

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

See the ScreenshotNeo API documentation for authentication and request options. Its clean-shot steps accept cookie or consent banners and remove more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be disabled. 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. ScreenshotNeo also offers an MCP server with take_screenshot, get_page_info, and capture_pdf tools for AI agents and MCP clients.

The Free plan includes 1,000 screenshots per month with no 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 1,000 free screenshots a month with no card.

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

Frequently Asked Questions

Does a server-side screenshot require a browser?

Yes. A browser renderer such as Puppeteer or Playwright turns the webpage into pixels; an HTTP request by itself only fetches content.

Can Puppeteer capture an element instead of a whole page?

Yes. Locate the element, wait for it to appear, then call its screenshot method.

Why can network-idle waiting fail on some sites?

Pages with long polling or streaming may not become network-idle. Wait for a required selector or an application-specific readiness signal instead.

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.

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.