I'm using the perl module Gtk2::WebKit
to create a browser and take a screenshot of a web page. There is no problem to take a screenshot of the visible portion of the page (the window size). However, how can I take a screenshot of the full page, even the part not visible in the window?
I think this is what you're looking for. I found it on Github.
screenshot.pl - Take a screenshot
Save a page as an SVG:
screenshot.pl --type svg http://www.google.com/
Save a page as a PDF:
screenshot.pl --output cpan.pdf http://search.cpan.org/
Save an element of a page taken from an XPath query as a PNG:
screenshot.pl --output ba.png --xpath 'id("content")' http://bratislava.pm.org/
Here is my GTK2 solution to this problem:
#!/usr/bin/perl
use Gtk2 -init;
use Gtk2::WebKit;
use Data::Dumper;
my $window = Gtk2::Window->new;
my $sw = Gtk2::ScrolledWindow->new;
my $view = Gtk2::WebKit::WebView->new;
my $factor = 0;
$window->set_default_size(Gtk2::Gdk->screen_width, Gtk2::Gdk->screen_height);
$window->set_border_width(1);
$sw->add($view);
$window->add($sw);
$window ->signal_connect( 'destroy' => \&delete_event );
$view->signal_connect( 'load-finished' => \&prepare_zoom);
$view->set_full_content_zoom(TRUE);
$view->signal_connect( 'size-allocate' => \&screenshot);
$view->open('http://stackoverflow.com/questions');
$window->show_all;
Gtk2->main;
#####################################
sub delete_event {
Gtk2->main_quit;
return FALSE;
}
#####################################
sub prepare_zoom {
$adj = $sw->get_vadjustment();
$factor = $adj->page_size/$adj->upper;
$view->set_zoom_level($factor);
}
sub screenshot {
return unless defined($window->window) && $factor>0;
my ($width, $height) = $window->window->get_size;
my $sWidth=$width*$factor;
my $gdkpixbuf = Gtk2::Gdk::Pixbuf->new ('rgb', 0, 8, $width, $height);
$gdkpixbuf->get_from_drawable($window->window,
undef, 0, 0, 0, 0, $width, $height);
$gdkpixbuf->save ("screenshot.jpg", 'jpeg', quality => 100);
#Gtk2->main_quit;
return FALSE;
}
This code works, but I'm not a GTK2 / Webkit expert, so I'm sure it can be written in a better way.
The idea behind it is simple:
NOTES
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With