PDF Viewer con Python e Gtk

Questo è un piccolo snippet per visualizzare in python un file PDF su Linux,
si basa su Pygtk, Glade, PyPoppler per la visualizzazione dei PDF.

Il file glade per l’interfaccia GTK 2.0 è il seguente: previewWindow.glade.tar


#!/usr/bin/python
# coding: UTF-8
#
# Copyright (C) Francesco Guarnieri 2010
# 

import sys
import gtk
import poppler

class PreviewWindow:
    def __init__(self, filename):
        uiFileName = "previewWindow.glade"

        # Usa GtkBuilder per creare l'interfaccia dal file XML
        try:
            builder = gtk.Builder()
            builder.add_from_file(uiFileName)
        except:
            self.error_message("Failed to load UI XML file: " + uiFileName)
            sys.exit(1)
        self.previewWindow = builder.get_object("previewWindow")
        self.previewScrolledWindow =  builder.get_object("previewScrolledWindow")
        self.previewDrawingArea = builder.get_object("previewDrawingArea")
        self.statusbar = builder.get_object("statusbar")

        builder.connect_signals({"on_previousToolbutton_clicked": self.on_previous,
                                 "on_nextToolbutton_clicked": self.on_next,
                                 "on_previewWindow_delete_event" : self.close,
                                 "on_previewDrawingArea_expose_event": self.__on_expose,
                                 "on_previewScrolledWindow_scroll_event": self.__on_scroll,
                                 "on_previewScrolledWindow_key_press_event": self.__on_key_press
                                })

        uri = "file://" + filename

        self.document = poppler.document_new_from_file(uri, None)
        self.n_pages = self.document.get_n_pages()

        self.pageNumber = 0
        self.scale = 1

        self.current_page = self.document.get_page(self.pageNumber)
        self.width, self.height = self.current_page.get_size()

        self.previewScrolledWindow.set_size_request(int(self.width * self.scale), int(self.height * self.scale))

        self.previewWindow.set_title(self.document.get_property("title"))

    def __refresh(self):
        self.pageNumber = self.pageNumber % self.n_pages
        self.statusbar.push(1, "Page %s of %s" % (self.pageNumber + 1, self.n_pages))
        self.current_page = self.document.get_page(self.pageNumber)
        self.previewDrawingArea.queue_draw()

    def on_previous(self, widget):
        self.pageNumber -= 1
        self.__refresh()

    def on_next(self, widget):
        self.pageNumber += 1
        self.__refresh()

    def __on_expose(self, widget, event):
        cr = widget.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.width, self.height)
        cr.fill()
        self.current_page.render(cr)

    def __on_key_press(self, widget, event):
        keyname = gtk.gdk.keyval_name(event.keyval)
        if keyname == "Page_Down":
            self.pageNumber -= 1
        elif keyname == "Page_Up":
            self.pageNumber += 1
        self.__refresh()

    def __on_scroll(self, widget, event):
        if event.state & gtk.gdk.CONTROL_MASK:
            if (event.direction == gtk.gdk.SCROLL_UP) and (self.scale > 0.25):
                self.scale -= 0.25
            elif (event.direction == gtk.gdk.SCROLL_DOWN) and (self.scale < 4):
                self.scale += 0.25
            self.previewDrawingArea.set_size_request(int(self.width * self.scale), int(self.height * self.scale))
        else:
            if event.direction == gtk.gdk.SCROLL_UP:
                self.pageNumber -= 1
            elif event.direction == gtk.gdk.SCROLL_DOWN:
                self.pageNumber += 1
        self.__refresh()
        return True

    def show(self):
        self.__refresh()
        self.previewWindow.show()
        gtk.main()

    def close(self, widget, other = None):
        self.previewWindow.destroy()

if __name__ == "__main__":
    previewWindow = PreviewWindow("/home/francesco/Desktop/reportlab-userguide.pdf")
    previewWindow.show()


Condividi:

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *