Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying PDF files with python3

I want to write a python3/PyGTK3 application that displays PDF files and I was not able to find a python package that allows me to do that.
There is pypoppler, but it looks outdated (?) and does not seem to support python3 (?)

Do you have any suggestions?

EDIT: Note, that I don't need fancy features, like pdf forms, manipulation or writing.

like image 349
Fabian Henze Avatar asked Mar 13 '12 10:03

Fabian Henze


People also ask

How do I read a PDF in Python 3?

Use the PyPDF2 Module to Read a PDF in Python We open the PDF document in read binary mode using open('document_path. PDF', 'rb') . PDFFileReader() is used to create a PDF reader object to read the document. We can extract text from the pages of the PDF document using getPage() and extractText() methods.


2 Answers

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/python3

import gi
gi.require_version('Poppler', '0.18')

from gi.repository import Poppler

document = 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/python3
import gi
gi.require_version('Poppler', '0.18')
gi.require_version('Gtk', '3.0')

from gi.repository import Poppler, Gtk

def 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()
like image 185
Fabian Henze Avatar answered Sep 21 '22 18:09

Fabian Henze


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.

like image 36
dumbmatter Avatar answered Sep 18 '22 18:09

dumbmatter