How to snapshot html

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật