1

I am using webitgtk6

#include <webkit/webkit.h>

Here are printing result functions

static void print_finished(WebKitPrintOperation *operation, gpointer user_data)
{
    g_print("Done\n");

    g_object_unref(user_data);
    g_object_unref(operation);
}

static void print_failed(WebKitPrintOperation *operation, GError *error, gpointer user_data)
{
    g_print("Failed %s\n", error->message);
    g_object_unref(user_data);
    g_object_unref(operation);
}

This function saves the page on a button click

static void save_page(GtkButton *button, WebKitPrintOperation *operation)
{
    const gchar *path = g_get_user_special_dir(G_USER_DIRECTORY_DOCUMENTS);
    gchar *filename = "simple";
    GtkPrintSettings *settings = gtk_print_settings_new();
    g_signal_connect(operation, "finished", G_CALLBACK(print_finished), settings);
    g_signal_connect(operation, "failed", G_CALLBACK(print_failed), settings);
    gtk_print_settings_set(settings, GTK_PRINT_SETTINGS_OUTPUT_BASENAME, filename);
    gtk_print_settings_set(settings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT, "pdf");
    gtk_print_settings_set(settings, GTK_PRINT_SETTINGS_OUTPUT_DIR, path);
    gtk_print_settings_set_paper_size(settings, gtk_paper_size_new(GTK_PAPER_NAME_A3));
    gtk_print_settings_set_printer(settings, "Print to File");
    webkit_print_operation_set_print_settings(operation, settings);
}

The on activate function

static void activate(GtkApplication *application, gchar *home)
{
    GtkWidget *widget = webkit_web_view_new();
    GtkWidget *parent = gtk_application_window_new(application);
    GtkWidget *child = gtk_overlay_new();
    GtkWidget *button = gtk_button_new_with_label("Start");
    GtkWindow *window = GTK_WINDOW(parent);
    GtkOverlay *overlay = GTK_OVERLAY(child);
    WebKitWebView *webview = WEBKIT_WEB_VIEW(widget);
    WebKitNetworkSession *session = webkit_web_view_get_network_session(webview);
    WebKitCookieManager *cookiejar = webkit_network_session_get_cookie_manager(session);
    const gchar *allowlist[] = {"https://www.example.com", "https://subdomain.example.com", NULL};
    gtk_overlay_set_child(overlay, widget);
    gtk_overlay_add_overlay(overlay, button);

    gtk_widget_set_halign(button, GTK_ALIGN_START);
    gtk_widget_set_valign(button, GTK_ALIGN_START);
    gtk_widget_set_size_request(button, 72, 48);
    gtk_widget_set_size_request(parent, 1024, 576);
    gtk_window_set_child(window, child);
    gchar *storage = g_strdup_printf("%ssession.db", home);
    gchar *uri = "https://bing.com";
    webkit_cookie_manager_set_persistent_storage(cookiejar, storage, WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE);
    webkit_web_view_set_cors_allowlist(webview, allowlist);
    webkit_web_view_load_uri((WebKitWebView *)webview, uri);

    g_free(home);
    g_free(storage);
    g_signal_connect(webview, "load-changed", G_CALLBACK(load_changed), NULL);
    g_signal_connect(button, "clicked", G_CALLBACK(save_page), webkit_print_operation_new(webview));
    gtk_window_present(window);
}

The main function

int main(int argc, char *argv[])
{
    gchar *home = g_strdup_printf("%s/foobar/", g_get_user_data_dir());
    GtkApplication *app = gtk_application_new("com.foo.bar", G_APPLICATION_DEFAULT_FLAGS);
    g_signal_connect(app, "activate", G_CALLBACK(activate), home);
    g_application_run((GApplication *)app, argc, argv);
    return 0;
}

save page runs but neither print_finished nor print_failed is reached and the pdf not created

I am expecting to see the file in ~/Documents/simple.pdf. I tried putting in

path manually /home/user//Documents/ it still doesn't create the pdf

I have also removed the line webkit_web_view_set_cors_allowlist(webview, allowlist); with the same result

2
  • 2
    Which line in the above code does the actual printing? Commented Jan 24 at 10:44
  • Lol apparently I deleted it while restructuring. webkit_print_operation_print(operation); Commented Jan 24 at 12:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.