Issues Viewing PDF's
Platform:开发者_运维技巧 Linux, GTK+
Tools: Python, PyGTK and Glade.
Problem:
- I'd like to write a program that is capable of displaying a PDF file.
Question:
- Which widget(s) and Python module(s) do I need?
Thanks for any suggestions!
look into the python poppler bindings.
I render pdf files in a simple dirty way. I copied the method used in the example for the python poppler gtk bindings
def load_pdf(self):
self.doc = poppler.document_new_from_file (uri, None)
# the number of pages in the pdf
self.n_pgs = self.document.get_n_pgs()
# the current page of the pdf
self.curr_pg = 0
# the current page being displayed
self.curr_pg_disp = self.document.get_page(self.curr_pg)
# the scale of the page
self.scale = 1
# the document width and height
self.doc_width, self.doc_height = self.curr_pg_disp.get_size()
def render_pdf(self):
cr = self.pdfda.window.cairo_create()
cr.set_source_rgb(1, 1, 1)
if self.scale != 1:
cr.scale(self.scale, self.scale)
cr.rectangle(0, 0, self.doc_width, self.doc_height)
cr.fill()
self.curr_pg_disp.render(cr)
def on_next_btn_clicked(self, widget, data=None):
if self.curr_pg < self.n_pgs:
self.curr_pg = self.curr_pg + 1
self.curr_pg_disp = self.doc.get_page(self.curr_pg)
self.render_page()
def on_prev_btn_clicked(self, widget, data=None):
if self.curr_pg > 0:
self.curr_pg = self.curr_pg - 1
self.curr_pg_disp = self.doc.get_page(self.curr_pg)
self.render_page()
Its not the best or prettiest but it works. I still have to add how to make it scrollable or center in the drawing area and stuff like that but there is a start.
you could also look into the evince python bindings, I do believe they have a widget you can use for to make pdf rendering easier. I'm on developing on windows so I haven't used it if there is one.
Not GTK but wxPython:
This example shows a PDF Viewer class, which handles things like Zoom and Scrolling. It requires python-poppler and wxPython >= 2.8.9.
- http://code.activestate.com/recipes/577195-wxpython-pdf-viewer-using-poppler/
http://pypi.python.org/pypi/ghostscript/
精彩评论