There are many situations where you might want to take screenshots of a webpage. Sure, there are browser extensions that do the job, but if you have to take screenshots of many web pages for archiving them, or generate them as part of an automated process, then you need a command line tool for the job. Here are three tools that generate web page screenshots from the command line.
Pageres-cli
Although this is not as popular as some of the other, more well-known tools, pageres-cli is easily the best tool for generating screenshots. Based on PhantomJS, and written in Node.js, it renders even the most complex web pages accurately.
In order to install pageres-cli, you must first install Node.js and NPM. Then, open up a terminal window and install it like so:
Having installed it, generating screenshots is easy as pie:
This gives you a screenshot in your current working directory, named
google.com-1366x768.png
, which as you might have guessed, is in the resolution 1366×768 and in the PNG format.
You can specify your own filename, format and resolution. Consider the following command:
That’s a long command, so let’s break it down for you:
google.com
andyahoo.com
are the URLs whose screenshots will be generated.1280x800
specifies the screen size in which the screenshot will be rendered.--format
specifies that the format that will be used. In this case we used the JPG format.--filename
specifies the directory where the screenshots will be stored and the format in which files will be named. In our case, we have specified that they should go into the “Pictures” directory, and the filenames should contain the date, followed by an underscore (_
), and further followed by the URL.
You can even specify different settings for different websites!
Finally, you might notice some websites giving you “lite” or “mobile” versions of web pages while using this tool. You can simply change the user-agent string so that it resembles a modern browser:
There are a variety of other options available; have a look at the project’s homepage.
cutycapt
While pageres-cli is great, it’s not to everyone’s tastes. Fortunately, there are other tools like cutycapt. It uses the QtWebkit library for rendering web pages.
On Ubuntu and Debian, you can install it with
sudo apt install cutycapt
; on other systems, you can compile it from source as described on their homepage.
You can take a screenshot with cutycapt like so:
cutycapt will try to detect the format of the output file from the filename. JPG, GIF, SVG and PNG formats are supported, among many others.
By default, cutycapt generates screenshots of the width 800×600. You can control the height and width of the screenshot, like so:
This will give you a screenshot of the resolution 1366×768.
Have a look at the man page by typing in
man cutycapt
on the terminal to see a list of options.
If you are facing problems with websites serving you “lite” or “mobile” versions, specify an user-agent string resembling a modern browser:
However, if you are trying to run cutycapt on a computer with no running X server (like most servers), it will fail to run, and you have to use
xvfb
in order to use it:wkhtmltoimage
wkhtmltoimage, which comes as part of wkhtmltopdf, is another tool to generate screenshots.
It should be available in your distribution’s repositories. On Debian/Ubuntu, you can get it with
sudo apt install wkhtmltopdf
. If it is not available on your distribution, you can always get the precompiled binaries or compile it from source.
One word of caution though: if you are planning on running wkhtmltoimage on an environment without a X server, you should grab the precompiled binary — the ones in the default repositories often do not support running without it.
To install the precompiled binaries, download the latest archive for your system, and run:
You can make screenshots of webpages by running:
Generating both JPG and PNG screenshots are supported.
You can set a custom width and height by specifying:
In certain cases, the screenshot may have a greater width than what has been specified. You can fix this by adding the
--disable-smart-width
flag.
The full list of options is available with
wkhtmltoimage --extended-help
.
wkhtmltoimage is fine for basic usage, but you should use the other tools if you need to take screenshots of complex webpages. There is no way to override the user-agent; and the sizes of PNG screenshots are rather huge.