Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl Gtk2::WebKit: how to do a full page screenshot

Tags:

webkit

perl

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?

like image 357
Julien Avatar asked Sep 30 '22 17:09

Julien


2 Answers

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/
like image 141
Chankey Pathak Avatar answered Oct 04 '22 21:10

Chankey Pathak


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:

  1. Load the page
  2. Get full page size and visible page size, then calculate the scale factor needed to see the full page content
  3. Scale the page using that scale factor
  4. Grab the screenshot

NOTES

  • Depends where you want to apply it, the scale factor calculation may need some corrections
  • You can crop the screenshot (to remove the blank areas) adjusting the values passed to get_from_drawable.
like image 29
Victor Henriquez Avatar answered Oct 04 '22 19:10

Victor Henriquez