Issue with URI on the same directory

This is my first topic, apologies in advance for any mistake.

This is one of my first scripts in Python (and also one of my first programming attempts :sweat_smile:).

Investigating an issue in Windows with paths, I found another issue in Linux.

See the following sample:

#!/usr/bin/env python3
# coding: utf-8

from urllib import request
import os, sys, cairo, gi
gi.require_version("Gtk", "3.0")
gi.require_version("Gst", "1.0")
from gi.repository import Gtk, Gdk, GLib, GObject, Gst
gi.require_version('Poppler', '0.18')
from gi.repository import Poppler

# ~ Gst.init(sys.argv)

class weird:
    def __init__(self):


        # ~ self.uri = Gst.filename_to_uri(os.path.abspath(sys.argv[1]))
        # ~ self.uri = GLib.filename_to_uri(os.path.abspath(sys.argv[1]))
        self.uri = 'file:' + request.pathname2url(os.path.abspath(sys.argv[1]))


        self.document = Poppler.Document.new_from_file(self.uri, None)
        self.n_pages = self.document.get_n_pages()
        self.page_selector = self.document.get_page(0) = Gtk.Window(Gtk.WindowType.TOPLEVEL) # deprecated

        self.vbox = Gtk.VBox()

        self.area = Gtk.DrawingArea()
        self.vbox.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(0, 0, 0, 1))
        self.area.connect("draw", self.on_draw)

        self.vbox.pack_start(self.area, True, True, 0)"destroy", Gtk.main_quit)'key-press-event', self.key_press_event)

        self.width, self.height =
        self.page_width, self.page_height= self.page_selector.get_size()

        self.surface = cairo.ImageSurface(cairo.FORMAT_RGB24,

    def key_press_event(self, widget, event):
        if event.keyval == Gdk.keyval_from_name("q"):

    def on_draw(self, widget, event):
        add_x = 0
        add_y = 0

        if (self.area.get_allocated_width()/self.page_width) < (self.area.get_allocated_height()/self.page_height):
            self.scale = self.area.get_allocated_width()/self.page_width
            add_y= (((self.area.get_allocated_height()-(self.page_height*self.scale))/2)/self.scale)
            self.scale = self.area.get_allocated_height()/self.page_height
            add_x= (((self.area.get_allocated_width()-(self.page_width*self.scale))/2)/self.scale)

        cr = Gdk.cairo_create( # deprecated

        cr.set_source_rgb(1, 1, 1)

        if self.scale != 1:
            cr.scale(self.scale, self.scale)

        cr.translate(add_x, add_y)
        cr.rectangle(0, 0, self.page_width, self.page_height)

    def gtk_main_quit(self, widget, event):

    def main(self):

we = weird()

No matter how self.uri is defined (Gst, GLib or urllib.request), the script only works if the script and the PDF document aren’t on the same directory (a symlink works around the issue).

But otherwise, I get the following error message:

TypeError: Couldn't find foreign struct converter for 'cairo.Context'

In that case, no key events work, not even the print() commands work.

Sorry for my extremely basic question, but what am I missing here?

Many thanks for your help.

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