brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: chrome --headless --screenshot takes a screenshot of a page from the command line

Chrome
~/tips/tip-chrome-headless-screenshot

google-chrome --headless --screenshot --window-size=1280,1696 https://example.com

Saves a PNG of a web page from the terminal, with no browser window.

I sometimes want a picture of a page without opening a browser window, for a script or a cron job. Chrome itself can do it, so I do not need extra tools for the simple case.

Chrome’s headless mode runs the browser without showing a window. The Chrome docs say to pass the --headless flag to a Chrome binary, and on Linux that looks like google-chrome --headless. The older headless blog post adds the screenshot flags:

google-chrome --headless --screenshot --window-size=1280,1696 https://example.com

That writes screenshot.png into the current working directory. The window size flag sets the viewport, so --window-size=412,732 gives a phone-sized picture.

The same post lists --print-to-pdf as a sibling flag. It is worth a try when you want a PDF of an article, and it takes the same kind of URL argument.

Caveats: the blog post I used is marked deprecated, and the docs say that since Chrome 132 the old headless mode only exists as a separate binary called chrome-headless-shell. The plain --headless flag now starts the new mode, which shares code with normal Chrome. I only checked the flags against that older post, so test the command on your own version before you put it in a script.

The post also says --disable-gpu is needed only on Windows. It also says that full-page screenshots are more involved than this one command. If you need the whole scrolling page, the DevTools full size screenshot is simpler.

Further reading: Chrome: Headless mode, Getting started with headless Chrome (deprecated post)

More Chrome tips chrome://net-export/ google-chrome --user-data-dir=/tmp/chrome-clean --no-first-run Windows and Linux: Ctrl+Shift+A Settings > Search engine > Manage search engines and site search > Site search > Add chrome://net-internals/#hsts All Chrome tips (12) All topics

Link