pythongtkgobject-introspection

How can I set a custom page size in a GtkPrintOperation?


I am using a GtkPrintOperation and need a custom page size for the draw-page signal.

Here is a Print Operation using python:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

print_op = Gtk.PrintOperation ()
print_op.run(Gtk.PrintOperationAction.PRINT_DIALOG, window)

Solution

  • Here is how to set a custom page size:

    def print_clicked (self, button):
        paper_size = Gtk.PaperSize.new_custom("1.5x", "918 x 1188", 918, 1188, Gtk.Unit.POINTS)
        page_setup = Gtk.PageSetup()
        page_setup.set_paper_size(paper_size)
        print_op = Gtk.PrintOperation ()
        print_op.set_embed_page_setup(True)
        print_op.set_default_page_setup(page_setup)
        print_op.set_n_pages( 1) # single page
        print_op.connect("draw-page", self.draw_page)
        print_op.run(Gtk.PrintOperationAction.PRINT_DIALOG, window)
    
    def draw_page (self, print_op, print_context, page_num):
        cr = print_context.get_cairo_context()
        #draw with cairo here