keepcalculator

06 Numbers and Powers

Screenshot Webpages via Command Line

Card No. 06·86656 · Updated · Standard formula, shown below

Yes, you can take screenshots of webpages directly from the command line using tools like wkhtmltoimage, cutycapt, or headless browsers such as Chrome and Firefox. You provide a URL and an output filename, and the tool renders the page and saves an image (PNG, JPEG, or PDF) without ever opening a visible browser window. This is ideal for automated testing, documentation, monitoring, or capturing pages that require JavaScript execution.

Why Use the Command Line for Screenshots?

Command-line screenshots are scriptable, repeatable, and lightweight. They fit naturally into cron jobs, CI/CD pipelines, or batch processing tasks where you need to capture many pages without manual effort. Unlike a browser extension, a CLI tool can be invoked from any language or shell script, and its output can be saved with a timestamped filename for archiving. It also runs on a server without a display, which is impossible with a normal desktop browser.

Available Tools and Their Core Differences

Several tools can capture webpages from the terminal, and they differ mainly in rendering engine, dependencies, and output formats.

  • wkhtmltoimage – Part of the wkhtmltopdf suite, uses the Qt WebKit engine. Simple to install on most Linux distributions, but does not support modern JavaScript features (ES6+) well.
  • CutyCapt – Also WebKit-based, similar limitations, but offers many command-line flags for controlling output size and quality.
  • Headless Chrome – The most modern option. Uses the full Chrome engine, supports the latest JavaScript, and can output PNG, JPEG, and PDF. Requires Chrome/Chromium installed.
  • Headless Firefox – Similar to Chrome, uses Gecko engine. Useful if you need Firefox-specific rendering.
  • PhantomJS – Older, now unmaintained, but still in some legacy scripts. Not recommended for new projects.

For most current needs, headless Chrome is the best balance of features and reliability.

How to Capture a Screenshot with Headless Chrome

Chrome has a built-in headless mode that can take screenshots directly. The basic command is:

chrome --headless --disable-gpu --screenshot=/path/to/output.png --window-size=1280,720 https://example.com

On many systems, the executable is named google-chrome, chromium, or chromium-browser. For example, on Ubuntu:

google-chrome --headless --disable-gpu --screenshot=example.png --window-size=1366,768 https://example.com

You can also specify the image format with the --screenshot extension (.png, .jpg), and set the device scale factor with --force-device-scale-factor=2 for retina-quality images.

Handling JavaScript and Delayed Loading

Many pages load content dynamically after the initial HTML. By default, headless Chrome waits until the page’s load event fires, but that may be too early. To wait longer, use the --virtual-time-budget flag, which simulates time passing and lets scripts finish:

chromium --headless --disable-gpu --virtual-time-budget=10000 --screenshot=after.png https://example.com

This waits up to 10 seconds of virtual time. For even more control, you can write a small script using Puppeteer (Node.js) or Playwright (Python/Node) to wait for specific elements or network conditions. Those libraries drive headless Chrome and give you full control over the capture process.

Common Problems and How to Solve Them

Here are typical issues you might encounter:

  • Blank or white screenshot – Often caused by the page requiring JavaScript that didn’t run before capture. Increase --virtual-time-budget or use a tool that waits for network idle.
  • “Failed to move to new PID” error – Chrome’s sandbox may conflict with the environment. Add --no-sandbox (but be careful on untrusted pages).
  • No such file or directory – Ensure the output directory exists and is writable.
  • Page is cut off – Use --window-size to set a larger viewport, or use a tool that captures the full page height by default (like wkhtmltoimage with --height).
  • Certificate errors – Add --ignore-certificate-errors if you are testing a site with a self-signed certificate.

Alternatives: wkhtmltoimage and CutyCapt

If you need a lightweight tool without a full browser, wkhtmltoimage is a good choice. Install it on Debian/Ubuntu with sudo apt install wkhtmltopdf (which includes wkhtmltoimage). Basic usage:

wkhtmltoimage --width 1024 --quality 90 https://example.com output.png

CutyCapt is similar but requires Xvfb (a virtual display) on servers. It is less convenient than headless Chrome, so it’s mostly used in legacy environments.

Automating Batch Screenshots

You can loop over a list of URLs in a shell script:

for url in $(cat urls.txt); do
  filename=$(echo $url | sed 's|https\?://||' | tr '/' '_')
  chromium --headless --disable-gpu --screenshot="$filename.png" "$url"
done

This saves each screenshot with a filename derived from the URL. For more robust automation, consider using a tool like Puppeteer with a script that waits for fonts and images to load.

Frequently Asked Questions

Can I capture a full-page screenshot (entire scrollable page)?

Headless Chrome’s basic --screenshot captures only the viewport. To get the full page, you need to use a library like Puppeteer or Playwright, which can set the viewport to the page’s full scroll height. Alternatively, wkhtmltoimage captures the full page by default, but it may not render modern JavaScript correctly.

How do I set a custom viewport size?

Use the --window-size=width,height flag with headless Chrome. For example, --window-size=1920,1080. For mobile emulation, you can also use --force-device-scale-factor and --window-size together, but true device emulation requires DevTools protocol tools like Puppeteer.

Does this work on Windows or macOS?

Yes. Chrome and Firefox are cross-platform, so the same commands work on Windows (using chrome.exe) and macOS (using chrome or chromium). The syntax is identical, though you may need to adjust the executable path.

Can I take a screenshot of a page that requires login?

Yes, but you need to handle authentication. One way is to use a browser profile that is already logged in, by launching Chrome with --user-data-dir=/path/to/profile. Alternatively, you can use Puppeteer to fill in login forms programmatically before taking the screenshot.

Is there a way to schedule screenshots at regular intervals?

Yes. Use a cron job on Linux/macOS or Task Scheduler on Windows to run your command at set times. For example, to capture a page every hour, add a line to your crontab: 0 * * * * /usr/bin/chromium --headless --screenshot=/path/to/shot.png https://example.com.

What is the difference between headless Chrome and Puppeteer?

Headless Chrome is a browser mode that runs without a UI and can be controlled via command-line flags. Puppeteer is a Node.js library that provides a higher-level API to control headless Chrome, allowing you to wait for elements, click buttons, and capture full-page screenshots with ease. For simple screenshots, the command line is enough; for complex interactions, use Puppeteer.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →