Creating Website Screenshots on Linux
Today a customer asked for automated screenshots of his website. So first thing to do was asking Google how it could be accomplished on a Linux WebServer. Most of the results referred to installing an X-Server, using Firefox and stuff. This sounded a bit tricky and quite frankly.. over the top.
The solution I came up with is (IMO) much easier and less prone to failures: WKHTMLTOPDF + Imagick Convert.
WKHTMLTOPDF uses QT with a Webkit Widget – it renders HTML pages inlcuding JavaScript like Apple’s Safari Browser. So creating a PDF from a website became very easy:
Usage: wkhtmltopdf -L 0 -T 0 -R 0 -B 0 http://mywebsite.com/ test.pdf
That’s all you need to create a PDF from your website including all images, javascript rendered contents and such. The parameters “-L -T -R -B” define the the border margin of the PDF document. The default value is 10mm – I prefer zero.
Now that we have a PDF of the website, we need to convert it into a JPG file. This can be acomplished using ImageMagick.
Usage: convert -density 600 test.pdf -scale 2000×1000 test.jpg
In case your webpage is pretty long it might happen that your PDF contains multiple pages. Imagick generates one JPG file per page named test-0.jpg, test-1.jpg and so on. All you need to do is combining the images with Imagick in a second step, which is also pretty easy and straighforward:
Usage: convert test-* -append single.jpg
That’s it… now you have a single image file from your website.