How can I take screenshots of webpages with Perl?

You could use WWW::Mechanize::Firefox to control a Firefox instance and dump the rendered page with $mech->content_as_png.

Be aware that setting it up can pose quite a challenge, though.

If all works as expected, you can simply use a script like this to dump images of the desired websites, but you should start Firefox and resize it to the desired width manually (height doesn’t matter, WWW::Mechanize::Firefox always dumps the whole page).

use WWW::Mechanize::Firefox;
use Path::Class qw/file/;

my $mech = WWW::Mechanize::Firefox->new(
  bufsize => 10_000_000, # PNGs might become huge

my $fh = file( 'test.png' )->open( '> :raw' );
print $fh $mech->content_as_png();

Leave a Comment