I needed to save web pages screenshots. The closest option to my initial environment was Fedora Linux 16 on Amazon EC2 is python-webkit2png.

To make it work on the headless server and render flashes I had to:

Get the base Fedora AMI running, I’ve selected the first one I’ve found (ami-7bf4c90f) and ran it as micro

yum install  wget unzip PyQt4 qtwebkit xorg-x11-server-Xvfb-1.11.4-3.fc16 dejavu-* liberation-*
rpm -ivh http://linuxdownload.adobe.com/adobe-release/adobe-release-i386-1.0-1.noarch.rpm
yum install flash-plugin
wget https://github.com/AdamN/python-webkit2png/zipball/master
unzip -x master
cd AdamN-python-webkit2png*
python setup.py install

And you should be ready to go

Test run it like this:

webkit2png  -W -o goo.png -w 20 -F javascript -F plugins -g 1280 1024 -x 1280 1024 http://www.wechoosethemoon.org/

And you should see that it doesn’t render flash. Thing is you have no browser on the server so plugin directories where webkit expects to find them are not there. Create one and link the flash plugin:

mkdir -p /usr/lib/mozilla/plugins && ln -s /usr/lib/flash-plugin/libflashplayer.so /usr/lib/mozilla/plugins/

redo the test above and you will see the flash rendering. but somehow –wait option doesn’t seem to work so the rendering doesn’t finish so you will only get progress bars when there is lots of data the initial swf pre-loads. It seems like a problem in webkit2png.py around line 219 (/usr/lib/python2.7/site-packages/webkit2png-0.8.2-py2.7.egg/webkit2png/webkit2png.py):

waitToTime = time.time() + self.wait
while time.time() < waitToTime and QApplication.hasPendingEvents():

If there are no QApplication pending events the while will break out and will not wait. I am not sure what the problem really is but changing it to this:

waitToTime = time.time() + self.wait
while time.time() < waitToTime:
if QApplication.hasPendingEvents():
makes the waiting work. I will open an issue on the github to let people know.


Update: Another issue with flash.
Seems like the window is resized too late and flash doesn’t update its geometry thus creating 800×600 screenshots. This fix seems to make things work better:
in the WebkitRenderer.render(), resize the window just after the helper is created:

       helper = _WebkitRendererHelper(self)
       helper._window.resize( self.width, self.height )

10 Thoughts on “Making web pages screenshots with webkit2png (flash included)

  1. Hi there,

    I am trying to replicate this.
    I have launched the same Amazon AMI (ami-7bf4c90f), and installed everything. Webkit2PNG works and outputs images for some simple webpages.

    However, when I try to take a screenshot of http://www.wechoosethemoon.org/ I get:

    webkit2png -W -o goo.png -w 20 -F javascript -F plugins -g 1280 1024 -x 1280 1024 http://www.wechoosethemoon.org/
    > loaded the Generic plugin
    > Xlib: extension “RANDR” missing on display “:1001111”.
    > QFont::setPixelSize: Pixel size QFont::setPixelSize: Pixel size <= 0 (0)

    and no image is produced.

    Do you recognize this? How did you solve it?

  2. Not really, the output is the same I get and the images are produced just fine. If you get images of normal pages but not flash sites then I would say it is a problem with flash plugin. First of all try to launch it not on Xvfb but on a real X (ssh forwarding will do just fine) to see how it renders. If it doesn’t, strace is your friend to find why it doesn’t load flash player.

    Can also be something stupid like network or permissions problem. Did you try other flash-based sites?

  3. Timothy Tocci on March 29, 2013 at 12:37 am said:

    Fedora only? Why not the ubiquitous Ubuntu?

  4. Any clue how to get this to work with google maps traffic layer?

  5. karanreddy on August 20, 2014 at 7:41 am said:

    ImportError: No module named PyQt4.QtCore

    getting this error and i have installed PYQt4 as well am using centos 6.5 (Virtualenv)

  6. Pingback: Python:Is it technically possible to take a screenshot of a website programmatically? – IT Sprite

Leave a Reply

Your email address will not be published. Required fields are marked *

Post Navigation