Displaying PDF files with python3 Displaying PDF files with python3 python-3.x python-3.x

Displaying PDF files with python3


It turns out, that newer versions of poppler-glib don't require bindings as such. They ship with GObject Introspection files and can therefore be imported and used as follows:

#!/usr/bin/python3import gigi.require_version('Poppler', '0.18')from gi.repository import Popplerdocument = Poppler.Document.new_from_file("file:///home/me/some.pdf", None)print(document.get_pdf_version_string())

That was easy, wasn't it? It took me hours to find that out ...

Note that one needs at least poppler-0.18, if one wants to import GTK as well.

Here is another minimal example with a GUI:

#!/usr/bin/python3import gigi.require_version('Poppler', '0.18')gi.require_version('Gtk', '3.0')from gi.repository import Poppler, Gtkdef draw(widget, surface):    page.render(surface)    document = Poppler.Document.new_from_file("file:///home/me/some.pdf", None)page = document.get_page(0)window = Gtk.Window(title="Hello World")window.connect("delete-event", Gtk.main_quit)window.connect("draw", draw)window.set_app_paintable(True)window.show_all()Gtk.main()


This post says that the latest development version of Evince (which I guess will become 3.4 shortly) supports embedding via PyGObject, which would probably work for your purposes.