GTK application to print a document on Windows OS

I have written a C application to print simple text on windows OS using GTK-4 . However I get status 2 (@GTK_PRINT_OPERATION_RESULT_CANCEL: The print operation has been canceled,

  • the print settings should not be stored.)

I tried print to pdf and print to a printer (cloud printer).
Print to PDF: This option allows the user to create a PDF file; however, the generated PDF file is corrupted when opened, indicating that printing did not occur successfully to PDF.
Print to Printer (Cloud Printer): Print jobs are queued in the cloud printer, which can be viewed via the printer UI, but the printer does not produce any printed output.

 #include <stdio.h>
 #include <stdlib.h>
 #include <glib.h>
 #include <cairo.h>
 #include <gtk/gtk.h>
 
 
 static void draw_page(GtkPrintOperation *operation, GtkPrintContext *context, int page_nr, gpointer user_data) {
     cairo_t *cr = gtk_print_context_get_cairo_context(context);
 
     cairo_move_to(cr, 10, 50);  // Set a position for the text
     cairo_set_source_rgb(cr, 0, 0, 0);  // Set text color to black
     cairo_show_text(cr, "Hello, printer!");  // Draw simple text
     cairo_stroke(cr);  // Complete the drawing
 }
 
 static void on_print_button_clicked(GtkWidget *widget, gpointer user_data) {
     GtkPrintOperation *print;
     GtkPrintOperationResult result;
     GError *error = NULL;
 
     print = gtk_print_operation_new();
     g_signal_connect(print, "draw-page", G_CALLBACK(draw_page), NULL);
 
     result = gtk_print_operation_run(print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
                                      NULL, &error);
     printf("The print status is: %d\n", result);
     if (result == GTK_PRINT_OPERATION_RESULT_ERROR) {
         g_print("Error: %s\n", error->message);
         g_error_free(error);
     }
 
     g_object_unref(print);
 }
 
 static void activate(GtkApplication *app, gpointer user_data) {
     GtkWidget *window;
     GtkWidget *button;
 
     window = gtk_application_window_new(app);
     gtk_window_set_title(GTK_WINDOW(window), "GTK 4 Print Demo");
     gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
 
     button = gtk_button_new_with_label("Print");
     gtk_window_set_child(GTK_WINDOW(window), button);
 
     g_signal_connect(button, "clicked", G_CALLBACK(on_print_button_clicked), NULL);
 
     gtk_window_present(GTK_WINDOW(window));
 }
 
 int main(int argc, char **argv) {
     GtkApplication *app;
     int status;
 
     app = gtk_application_new("com.example.GtkPrintDemo", G_APPLICATION_DEFAULT_FLAGS);
     g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
 
     status = g_application_run(G_APPLICATION(app), argc, argv);
 
     g_object_unref(app);
     printf("The application status is: %d\n", status);
     return status;
}

What are the features that are not supported or limited on windows w.r.t printer capabilities ? is there a documentation for the same ?

Hi @debugDynamo,

You should add a call to gtk_print_operation_set_n_pages:

     print = gtk_print_operation_new();
     g_signal_connect(print, "draw-page", G_CALLBACK(draw_page), NULL);

     gtk_print_operation_set_n_pages (print, 1); 

     result = gtk_print_operation_run(print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
                                      NULL, &error);

@lb90 Thank you so much for your time.
With your suggestion now I am getting a printout with text ! with result being
* @GTK_PRINT_OPERATION_RESULT_APPLY: The print settings should be stored.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.