[webkit-gtk] Taking full webpage screenshots

Emmanuel Rodriguez emmanuel.rodriguez at gmail.com
Thu Aug 11 03:44:40 PDT 2011


On Thu, Aug 11, 2011 at 11:28, Chandra Siva <chandra at zappli.com> wrote:
> Hi:
> I am attempting to take a full webpage screenshot of a browser (uzbl) that
> uses WebKitGTK+ similar to the way CutyCapt
> (http://cutycapt.sourceforge.net/) does it with WebKitQT
> This is the code I have so far and with this I am only able to get the
> visible parts of the browser in the screenshot. What is the best way to get
> a snapshot of the entire webpage ?

I have a github project where I made a few WebKit hack just for fun.
On of the hacks was about grabbing a screenshot of a web page through
WebKit

Take a look at this files:

https://github.com/potyl/Webkit/blob/master/screenshot.c - simple c
version (saves only as PDF)
https://github.com/potyl/Webkit/blob/master/screenshot.pl  - a bit
more advanced perl version (saves as all cairo supported formats)
https://github.com/potyl/Webkit/blob/master/screenshot.sh - simple
framebuffer wrapper to run without popping a window

If you figure out a way to take a screenshot without the need of a
DISPLAY (X Server) please let me know. You should be able to modify
the C program to support other output formats by simply changing the
cairo surface.

The C version of my script is pasted here (for the mail archives)

#include <gtk/gtk.h>
#include <webkit/webkit.h>
#include <libsoup/soup.h>
#include <cairo-pdf.h>


static void
save_as_pdf (GtkWidget *widget, const char *filename) {
    GtkAllocation allocation;

    gtk_widget_get_allocation(widget, &allocation);
    cairo_surface_t *surface = cairo_pdf_surface_create(
        filename,
        1.0 * allocation.width,
        1.0 * allocation.height
    );

    cairo_t *cr = cairo_create(surface);
    gtk_widget_draw(widget, cr);
    cairo_destroy(cr);
    cairo_surface_destroy(surface);
}


static void
load_status_cb (GObject* object, GParamSpec* pspec, gpointer data) {
    WebKitWebView *web_view = WEBKIT_WEB_VIEW(object);
    WebKitLoadStatus status = webkit_web_view_get_load_status(web_view);
    if (status != WEBKIT_LOAD_FINISHED) {
        return;
    }

    save_as_pdf(GTK_WIDGET(web_view), (const gchar *) data);

    gtk_main_quit();
}


int
main (int argc, gchar* argv[]) {
    gtk_init(&argc, &argv);

    if (argc < 2) {
        printf("Usage: URI [filename]\n");
        return 1;
    }
    const gchar *uri = argv[1];
    const gchar *filename = argc > 2 ? argv[2] : "a.pdf";

    if (!g_thread_supported()) {g_thread_init(NULL);}

    WebKitWebView *web_view = WEBKIT_WEB_VIEW(webkit_web_view_new());
    g_signal_connect(web_view, "notify::load-status",
G_CALLBACK(load_status_cb), (gpointer) filename);

    GtkWidget *offscren = gtk_offscreen_window_new();
    gtk_container_add(GTK_CONTAINER(offscren), GTK_WIDGET(web_view));
    gtk_widget_show_all(offscren);

    webkit_web_view_load_uri(web_view, uri);
    gtk_main();

    return 0;
}

-- 
Emmanuel Rodriguez


More information about the webkit-gtk mailing list