This isn't trivial. I'd probably do it using Xvfb, but the overhead is
not small. Below is a small shell script which should serve as a
start.
How it works: we run Xvfb, which produces a new X display which is
mapped in
memory and not associated with a physical screen. We then set the
DISPLAY
variable to refer to this screen, and run mozilla, passing it the URL
to view.
At this stage we *should* wait for it to display the page in some
sensible
manner, but for this example we just sleep for five seconds. Then, we
dump
the contents of the display using xwd -root -screen and pass the
output of
that into xwdtopnm and cjpeg, to convert it to a JPEG. If you wanted a
small thumbnail, you'd want to invoke pnmscale in the pipeline between
xwdtopnm and cjpeg. Some other notes: I invoke Mozilla using a profile
called `test', which is set up to not display any toolbars and so
forth, and
to avoid clashing with my real Mozilla profile. Various other options
in
the script are probably not necessary, but were handy for getting it
running as a prototype.
This script is quite hard work for the server to run. Mozilla itself
is a
big, slow, inefficient program. If you want to use this, you'd
certainly
want to have Mozilla sitting around in memory, and use the mozilla
-remote
command to tell the existing Mozilla to view new URLs. But that has
the
problem that every so often Mozilla will pop up a dialog box or
something
which will spoil the image. To do this really seriously, I'd recommend
writing a little X program which embeds the Mozilla rendering engine.
This
could also do the screen dumping and so forth, and you could just feed
it
URLs in reasonable certainty that it will do everything properly.
The script itself:
#!/bin/sh
set -x
set -e
url=$1
if [ -z $url ] ; then
echo "must specify a URL" 1>&2
exit 1
fi
Xvfb :5 -fp "unix/:-1" -screen 0 800x600x24 &
xvfb_pid=$!
DISPLAY=:5
export DISPLAY
#xdpyinfo
mozilla -height 600 -width 800 -P test -geometry +0+0+800x600 $url &
mozilla_pid=$!
xclock&
sleep 5
xwd -screen -root | xwdtopnm | cjpeg > fish.jpg
set +e
kill $mozilla_pid
kill $xvfb_pid |