Displaying PDF files with python3

老子叫甜甜 提交于 2019-12-03 09:34:52

问题


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.


回答1:


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

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

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()



回答2:


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.



来源:https://stackoverflow.com/questions/9682297/displaying-pdf-files-with-python3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!