Gtk.PrintOperationAction.PRINT still shows print dialog

Hello everyone,
I’m trying to print a PDF file without showing the print dialog to the user. I’m using Gtk.PrintOperation and Gtk4. I saw here (Gtk.PrintOperationAction) that the GTK_PRINT_OPERATION_ACTION_PRINT start to print without showing the print dialog, I implemented it but I’m still having the dialog. Here is my code :

def draw_page(self, operation=None, context=None, page_nr=None):
        ctx = context.get_cairo_context()
        
        document = Poppler.Document.new_from_file("file:///home/eric/test_print_2.pdf")
        page = document.get_page(page_nr)
        width, height = page.get_size()
        
        ctx.set_source_rgb(1, 1, 1)
        ctx.paint()

        # Use cairo to draw PDF
        cr = ctx
        scale_factor = 1.0
        cr.scale(scale_factor, scale_factor)

        page.render(cr)


    def print_file(self, *args):
        print("print file activated")
        print_operation = Gtk.PrintOperation()

        page_setup = Gtk.PageSetup.new_from_file("/home/eric/page_setup.conf")
        print_settings = Gtk.PrintSettings.new_from_file("/home/eric/print_settings.conf")

        print_operation.set_default_page_setup(page_setup)
        print_operation.set_print_settings(print_settings)

        document = Poppler.Document.new_from_file("file:///home/eric/test_print_2.pdf")
        print_operation.set_n_pages(document.get_n_pages())

        print_operation.connect("draw-page", self.draw_page)
        print_operation.run(Gtk.PrintOperationAction.PRINT, self.props.active_window)

I’m sure about file’s paths and permissions (“–filesystem=host”).

Can anyone help me ? Thank you !

Hi @Matthieu_Lorier,

I believe that happens because GTK uses the print portal under the hood, see printing: ACTION_PRINT and ACTION_PRINT_DIALOG are handled identically (#5726) · Issues · GNOME / gtk · GitLab

Try running with GDK_DEBUG=no-portals

Hi and thank you for the response! I tried to add the following lines to my flatpak manifest:

"build-options" : {
        "env" : {
            "GDK_DEBUG" : "no-portals"
        }
    }

But the dialog still appears.

Those are build options; the GDK_DEBUG environment variable should be set when running the application—but it’s a debugging tool, and should not be used in normal operations.

In practice, what you want to do is not possible with recent GTK, as we default to using the portals across the board on Linux.

So it’s impossible? They are no solutions for it?

It’s not “impossible”, but it requires you to use the GtkPrintDialog API instead of the print operation API. If you pass a NULL GtkPrintSetup object to gtk_print_dialog_print(), for instance, the GtkPrintDialog will attempt to print without directly without user interaction.

Thank you for response, but in my case, I use two .conf file:

        page_setup = Gtk.PageSetup.new_from_file("/home/eric/page_setup.conf")
        print_settings = Gtk.PrintSettings.new_from_file("/home/eric/print_settings.conf")

        print_operation.set_default_page_setup(page_setup)
        print_operation.set_print_settings(print_settings)

Any ideas to create a Gtk.PrintSetup from these files? I already searched for it but I found nothing.

You already are creating a GtkPrintSetup.

Instead of creating a GtkPrintOperation, use the GtkPrintDialog API:

GtkPageSetup *setup = gtk_page_setup_new_from_file ("page-setup.conf");
GtkPrintDialog *dialog = gtk_print_dialog_new ();

gtk_print_dialog_print (dialog, your_app_window,
                        setup,
                        NULL,
                        callback, callback_data);

Then, inside callback, you are going to call:

g_autoptr (GError) error = NULL;
g_autoptr (GOutputStream) output =
   gtk_print_dialog_print_finish (dialog, result, &error);

if (error)
  do_something_on_error (error);
else
  print_on_stream (output);

Thank you for your answer, but I think there’s a mistake:

Traceback (most recent call last):
  File "/app/share/test/test/main.py", line 113, in print_file
    dialog.print_(None, page_setup, None, on_print_finished)
TypeError: argument setup: Expected Gtk.PrintSetup, but got gi.repository.Gtk.PageSetup

It looks like Gtk.PrintDialog.print_() expects a Gtk.PrintSetup, not a Gtk.PageSetup. Do you know how to properly create a Gtk.PrintSetup from my page_setup.conf and print_settings.conf files?

Thank you for taking time to help me :slight_smile:

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