How to Capture a Website Screenshot in Linux

You might be wondering how to capture a website screenshot (as thumbnail) but
you dont know how to do it. Although PHP GD library is available, it is just
not possible to do the task with php. You will need a linux server running xwindow
system and a browser like KDE konquerer or firefox. In this tutorial i am going
to show you how to capture a website screenshot with khtml2png
in a Centos/RHEL server. I am documenting here the experiment i conducted in
my VPS so that my work could be helpful.

What is Needed?

Linux Server with Centos/RHEL

Khtml2Png (http://khtml2png.sourceforge.net)

ImageMagick (http://imagemagick.org)

KDE Desktop Environment

Cmake – http://www.cmake.org

Screenshot Capturing Basics

Most linux servers will usually not have a graphics card or output device like
monitor. This is usually called headless server and the alternate way is using
a Xvfb or Vncserver (Xserver with virtual frame buffer). It is basically a virtual
display that doesnt need a graphics device and monitor. Anything written to
Xvfb, can be captured as screenshot. In this tutorial, I will be using Xvfb
without making use of khtml2png and it is the most simple method of doing it.

First install Xvfb server and ImageMagick

yum install Xvfb ImageMagick

then,

yum install firefox

start the virtual display

> Xvfb :2 -screen 0 1024x768x24&

// This starts the virtual server with display no 2, screen no 1 on resolution
1024×768 with color depth of 24 bit.

Run the firefox on the virtual display in the command line

> DISPLAY=:2 firefox http://www.example.com

Wait for 5 seconds until, it loads and then capture a screenshot with ImageMagick’s
import tool

> import -window root example.png

once this is done, you can resize the images later with convert
tool

Using Khtml2PNG

khtml2png is another way of capturing a website screenshot of specified width
and height from Xvfb virtual server. More information can be found at http://khtml2png.sourceforge.net.

khtml2png requires KDE, gcc compiler, cmake and kde-devel libraries,

If you have a new linux server, prepare your server with c compiler and other
utilities

Install Basic Tools

yum install gcc gcc-c++ automake autoconf nano zlib zlib-devel

Install KDE desktop environment

yum groupinstall "X Window System" "KDE (K Desktop Environment)"

Install KDE developer libraries

yum install kdelibs kdelibs-devel

Install Xvfb Virtual Frame Buffer

yum install Xvfb xorg xorg-x11-font*

Install Cmake

Cmake is cross plaform make utility. You cannot install using yum so you have
to download the source files and compile it.

wget <path/of/cmake.tar.gz>
tar zxf cmake-2.7.1.tar.gz
cd cmake-2.7.1
 ./bootstrap
make
make install

Install Khtml2PNG

Download the source files from sourceforge.

wget <path/to/khtml2png-2.7.5.tar.gz>
tar xzf khtml2png-2.7.5
 ./configure
make install

Final Steps

Start Xvfb virtual server, then launch the khtml2png to grab a website screenshot

> Xvfb :2 -screen 0 1024x768x24&
> export DISPLAY=localhost:2.0

then run khtml2png2

> khtml2png2 --sw 200 --sh 150 http://www.example.com example.png

–sh and –sw indicates both scaled width and height

To view the image in the browser copy those created screenshot to apache /var/www/html
and point your browser to http://your.ip.address/example.png

If you want khtml2png run as daemon you might want to install
kthmld available from sourceforge

Common Errors

I am documenting the common errors i got and how i fixed those. Few i was able
to fix while others i couldnt fix.

khtml2png2: cannot connect to X server

[This means you have not assigned the display where Xvfb is running. Try export
DISPLAY=localhost:2.0
in the commandline

Couldn’t open RGB_DB ‘/usr/share/X11/rgb’

[Xvfb might throw this error if you set the resolution to 32 bit color depth.
Lower the resolution to 24 or 16bit color.

Example: Xvfb :2 -screen 0 800x600x24&]

error opening security policy file /usr/lib/xserver/SecurityPolicy

[Login as a user other than root]

Could not init font path element /usr/share/X11/fonts/TTF/, removing
from list!

Could not init font path element /usr/share/X11/fonts/OTF, removing from list!

Could not init font path element /usr/share/X11/fonts/Type1/, removing from
list!

Could not init font path element /usr/share/X11/fonts/CID/, removing from list!

Could not init font path element /usr/share/X11/fonts/100dpi/, removing from
list!

Could not init font path element /usr/share/X11/fonts/75dpi/, removing from
list!

[Try yum install xorg-x11-font*]

CMake Error: The following variables are used in this project, but
they are set to NOTFOUND.

Please set them or make sure they are set and tested correctly in the CMake
files:

QT_INCLUDE_DIR (ADVANCED)

[Install qt and qt-devel packages. If that
doesnt work, try rebooting the server and this might go off! ]

Enjoy!

Similar Posts:

Tags:

Balakrishnan Prabhu

Mr. Balakrishnan Prabhu is the founder of Corpocrat magazine. He is also the founder of Best Citizenships (BC), assisting wealthy individuals with with global citizenship and residency programs in Europe. His other interests are Linux, Machine learning, Wordpress, etc. You can contact him here

  • Great summary you have here. But i have the font problem, but cant find the package i need.

    Im running Debian 4.0 Etch.

    Debian-40-etch-64-LAMP:~/khtml2png-2.7.5# apt-get install xorg-x11-font
    Reading package lists… Done
    Building dependency tree… Done
    E: Couldn’t find package xorg-x11-font

  • Actually,we have already implement it, and it also support capture multiple website screenshot,look at demo at: sitegif.com

  • Its almost grateful to my new project.

  • It’s the first time I commented here and I must say you give genuine, and quality information for other bloggers! Good job.
    p.s. You have a very good template . Where did you find it?

  • apps25

    I keep getting this error…
    error opening security policy file /usr/lib/xserver/SecurityPolicy

    I have switched to another user other than root.. any ideas?

  • Eddie Lo

    You know what? I do hit all the common error that you mentions and you nailed them all. It is working nicely now! Thanks a lot!

  • Black Tea

    Couldn’t open RGB_DB ‘/usr/share/X11/rgb’
    ###########################################
    i recover it with install some other xorg rpm .

    this is my list

    [root@www ~]# rpm -aq | grep xorg
    xorg-x11-fonts-base-7.1-2.1.el5
    xorg-x11-server-utils-7.1-4.fc6
    xorg-x11-xkb-utils-1.0.2-2.1
    xorg-x11-utils-7.1-2.fc6
    xorg-x11-drv-keyboard-1.1.0-3
    xorg-x11-server-Xvfb-1.1.1-48.67.el5
    xorg-x11-drv-void-1.1.0-3.1
    xorg-x11-drv-mouse-1.1.1-1.1
    xorg-x11-server-Xorg-1.1.1-48.67.el5
    xorg-x11-proto-devel-7.1-13.el5
    xorg-x11-xfs-1.0.2-4
    xorg-x11-filesystem-7.1-2.fc6
    xorg-x11-font-utils-7.1-2
    xorg-x11-drv-vesa-1.3.0-8.1.el5
    xorg-x11-drv-evdev-1.0.0.5-3.el5

  • johann

    You seem to have got it working on CentOS. I thought I got it working as well, until I encountered some (actually many) websites that would cause khtml2png to crash:

    /usr/local/bin/khtml2png2 –width 1024 –height 768 –time 42 –disable-js –disable-java –disable-plugins –disable-redirect –disable-popupkiller http://www.google.com/ google.png
    terminate called after throwing an instance of ‘DOM::DOMException’
    KCrash: Application ‘khtml2png2’ crashing…

    DOMException sounds like something coming from the KDE. I wonder if you ever encountered this and found a solution?

  • Michael P. Shipley

    When I try to install firefox I get this error message:

    Error: Missing Dependency: udev >= 034-2 is needed by package hal

  • Mario GS

    Hi, I was wondering if there is a way to capture the browser content without the borders?