moz-headless-screenshot

July 29th, 2010

On a personal project of mine, I have a need to generate thumbnails of web pages. Until recently my solution was to use Firefox running in Xvfb, a virtual X server. This is not an ideal solution, as it requires you to have lots of X client libraries installed on your server. Additionally, Firefox is not intended for this purpose, so there are lots of ways for things to go wrong.

Because of this, I’d been following Chris Lord‘s work on the offscreen branch of Mozilla for some time, but never tried it out until recently. The offscreen branch provides a widget backend for Mozilla that can render web content to an offscreen buffer. Chris wrote it in support of Clutter, which is a pretty neat use case. Conveniently, he also provided a sample embedding client application called moz-headless-screenshot. This is a simple command line tool that takes a URL, image size, and output filename and generates a PNG screenshot of the webpage. This being exactly what I wanted, and having my poorly-written Firefox+Xvfb solution fall apart due to a server migration, I decided to give his solution a shot.

I hit a few speed bumps on the way, since there wasn’t much documentation to be found on actually building and using moz-headless-screenshot. I’ve attempted to fix this my providing detailed steps and a Makefile in my own moz-headless-screenshot repository. I’ve also modified the code slightly such that it’s easier to run (at least in my use case). I have heard from others over the years that have this same need, so hopefully someone else finds it useful!

6 Responses to “moz-headless-screenshot”

  1. therube Says:

    If I’m understanding, this should work similarly for a Windows user:

    Nirsoft: [url=http://www.nirsoft.net/utils/web_site_screenshot.html]SiteShoter – Take a screenshot of a Web site [/url]

    “SiteShoter is a small utility that allows you to save a screenshot of any Web page into a file. It automatically creates hidden window of Internet Explorer, loads the desired Web page, and than save the entire content of the Web page into an image file (.png, .jpg, .tiff, .bmp or .gif).

    You can also use SiteShoter to convert .html file on your local drive into image file.

    You can use SiteShoter in user interface mode, or alternatively, you can run SiteShoter in command-line mode without displaying any user interface.”

  2. Rob Helmer Says:

    This is very useful to me, thanks for blogging about this!

    For work I put together a website-to-PDF script using Firefox and Xvnc on Linux (easier to debug problems than Xvfb IMHO) but it’s a PITA all around, it’d be nice to have fewer moving parts for sure.

  3. voracity Says:

    I’ve been using wkhtmltopdf for, well, exactly what the name says. That uses Webkit, but it would be wonderful to have something Gecko based.

  4. woro Says:

    Hmm, what’s wrong with gnome-web-photo which is also using Gecko (in certain builds)?

  5. tmielczarek Says:

    gnome-web-photo has additional dependencies on Gnome libs which I don’t want to install. Also, it still needs an X server, AFAICT. (Although if it was built against the offscreen branch, it might not.)

  6. Chris Lord Says:

    Awesome, glad some people have found use for it 🙂 Would be great if someone picked up the work and managed to get it into master…