My operating system is CentOS.
I need to add webpage screenshot functionality for my server.
I don’t need to display web pages on my desktop or window.
And I hope it can always exist, so as to reduce the time required for program initialization each time.
My current solution is to use wkhtmltoimage in system().
It’s very simple, but too slow and inelegant.
Every time I generate an image, I have to use the system function to call it.
So… I started my second try.
I created a hidden window that contains WebView using libwebkit2gtk, and passed the HTML to WebView.
When the OnLoad event is triggered, call the screenshot function to get image, and save it in file.
This is my code:
void web_view_snapshot_callback(GObject* source_object, GAsyncResult* res, gpointer user_data) {
printf("%s:Inn", __func__);
cairo_surface_t* surface = webkit_web_view_get_snapshot_finish((WebKitWebView*)source_object, res, NULL);
if (surface) {
int width = cairo_image_surface_get_width(surface);
int height = cairo_image_surface_get_height(surface);
GdkPixbuf* pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, width, height);
if (pixbuf) {
if (gdk_pixbuf_save(pixbuf, "screenshot.png", "png", NULL, NULL)) {
printf("%s:Save Successn", __func__);
}
else {
printf("%s:gdk_pixbuf_save Failedn", __func__);
}
g_object_unref(pixbuf);
pixbuf = NULL;
}
else {
printf("%s:gdk_pixbuf_get_from_surface Failedn", __func__);
}
cairo_surface_destroy(surface);
surface = NULL;
}
else {
printf("%s:webkit_web_view_get_snapshot_finish Failedn", __func__);
}
gtk_main_quit();
}
void web_view_snapshot(WebKitWebView* web_view) {
printf("%s:Inn", __func__);
webkit_web_view_get_snapshot(web_view, WEBKIT_SNAPSHOT_REGION_FULL_DOCUMENT, WEBKIT_SNAPSHOT_OPTIONS_NONE, NULL, web_view_snapshot_callback, NULL);
}
void web_view_on_changed(WebKitWebView* web_view, WebKitLoadEvent load_event, gpointer user_data) {
switch (load_event) {
case WEBKIT_LOAD_FINISHED: {
printf("%s:WEBKIT_LOAD_FINISHEDn", __func__);
web_view_snapshot(web_view);
break;
}
default:
printf("%s:%dn", __func__, load_event);
break;
}
}
void window_destory(GtkWidget* widget, GtkWidget* window) {
gtk_main_quit();
}
int main(int argc, char* argv[]) {
gtk_init(&argc, &argv);
printf("gtk_initn");
WebKitWebView* web_view = WEBKIT_WEB_VIEW(webkit_web_view_new());
GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 1400, 0);
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(web_view));
g_signal_connect(window, "destroy", G_CALLBACK(window_destory), NULL);
g_signal_connect(web_view, "load-changed", G_CALLBACK(web_view_on_changed), NULL);
gtk_widget_show_all(window);
gtk_widget_hide(window);
gchar localfile_realpath[512] = { 0 };
gchar html_localpath[512] = { 0 };
realpath("./local.html", localfile_realpath);
sprintf(html_localpath, "file://%s", localfile_realpath);
printf("html_localpath:%sn", html_localpath);
webkit_web_view_load_uri(WEBKIT_WEB_VIEW(web_view), html_localpath);
g_object_unref(web_view);
gtk_main();
return 0;
}
It has to be said that the effect of this solution is very satisfactory, except that it requires the use of GTK.
In fact, this program can only run on the GNOME desktop environment, and I cannot use SSH connected terminals to run the program, so this solution is still not the best solution.
Therefore I turned my attention to WebKit itself, perhaps it could solve my problem.
I know libwebkit2gtk is a port for WebKit.
So… May I need to implement the Webkit port myself for snapshot website?
I don’t know if this idea is feasible.
Even if this idea is feasible, it may still take me a lot of time.
I feel lost.
Can anyone help me?
4