header ads

How to Take Screenshots of Webpages from the Command Line | Computer

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:
sudo npm install -g pageres-cli
Having installed it, generating screenshots is easy as pie:
pageres google.com
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:
pageres google.com yahoo.com 1280x800 --format=jpg --filename="Pictures/<%= date %>_<%= url %>
That’s a long command, so let’s break it down for you:
  • google.com and yahoo.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!
pageres [google.com 1280x800] [yahoo.com 800x600]
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:
pageres echo.opera.com --user-agent='Mozilla/5.0 (X11; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0'
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 --url=google.com --out=google.png
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:
cutycapt --url=google.com --out=google.png --min-width=1366 --min-height=768
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:
cutycapt --url=... --out=... --user-agent='Mozilla/5.0 (X11; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0'
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:
xvfb-run --server-args="-screen 0, 1024x768x24" cutycapt --url=... --out=...

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:
sudo tar -xf wkhtmltox-*.tar.xz -C /opt
sudo mkdir -p /usr/local/lib /usr/local/share/man/man1
sudo ln -s /opt/wkhtmltox/bin/wkhtmltoimage /usr/local/lib
sudo ln -s /opt/wkhtmltox/bin/wkhtmltopdf /usr/local/lib
sudo ln -s /opt/wkhtmltox/share/man/man1/wkhtmltoimage.1.gz /usr/local/share/man/man1
sudo ln -s /opt/wkhtmltox/share/man/man1/wkhtmltopdf.1.gz /usr/local/share/man/man1
You can make screenshots of webpages by running:
wkhtmltoimage google.com google.jpg
Generating both JPG and PNG screenshots are supported.
You can set a custom width and height by specifying:
wkhtmltoimage --height 1200 --width 800 google.com google.png
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.

Post a Comment

0 Comments