2

This is a followup from here. I am trying to reload a pdf file. Basically I want to watch the pdf file and as it changes, reload it in the viewer. I could not find any documentation for python+gtk3 and evince.

#!/usr/bin/env python

from gi.repository import Gtk, Gdk
from gi.repository import EvinceDocument
from gi.repository import EvinceView
import os,sys

if (len(sys.argv) is not 2):
    print "Usage: "+sys.argv[0]+" file.ext"
    sys.exit(0)
else:
    docFile=os.path.abspath(sys.argv[1])

#==========================================================
# Evince viewer class
class EvinceViewer:

    #------------------------------------------------------
    # constructor to build GUI and hook up function
    def __init__(self):

        # create main window
        self.window = Gtk.Window()
        # set title, size, position
        self.window.set_title("Evince")
        self.window.set_default_size(800,600)
        # connect destroy and delete events to quit
        self.window.connect('destroy', Gtk.main_quit)
        self.window.connect('delete-event', 
                Gtk.main_quit)
        self.window.connect("key-press-event", 
                self.keypress)

        # scrolled window for the viewer
        scroll = Gtk.ScrolledWindow()
        self.window.add(scroll)

        # evince document
        EvinceDocument.init()
        doc = EvinceDocument.Document.factory_get_document(
                'file://'+docFile)
        # evince view
        self.view = EvinceView.View()
        # evince model
        self.model = EvinceView.DocumentModel()
        self.model.set_document(doc)
        self.view.set_model(self.model)

        # add to scroll window
        scroll.add(self.view)
        self.window.show_all()
        self.fullscreen=False

    #------------------------------------------------------
    # handling keyboard events
    def keypress(self,widget,event):
        keyname = Gdk.keyval_name(event.keyval)
        ctrl = event.state & \
                Gdk.ModifierType.CONTROL_MASK

        if ctrl:

            if keyname=='r':
                self.view.reload()

            elif keyname == 'Return':
                if self.fullscreen == False:
                    self.fullscreen=True
                    self.window.fullscreen()
                else:
                    self.fullscreen=False
                    self.window.unfullscreen()

            elif keyname=='q':
                Gtk.main_quit()

    def main(self):
        Gtk.main()

if __name__ == "__main__":
    evinceViewer = EvinceViewer()
    evinceViewer.main()

The reload function does not seem to do the job. What is going wrong? On calling it, the document viewer displays a messed up version of the pdf file.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Vijay Murthy
  • 841
  • 2
  • 9
  • 19

1 Answers1

4

According to http://git.gnome.org/browse/evince/tree/libview/ev-view.c, ev_view_reload and ev_view_reload_page only redraw the page; they don't complete re-read the document. In your case, if you change the file without letting the library know, it'll assume that the file hasn't changed and attempt to render the document using reference tables from memory, causing it to behave erratically. If you change the document, you are going to have to reload the document itself.

To do this (lines 63-64):

if keyname=='r':
    self.model.get_document().load('file://'+docFile) # <- ADD THIS LINE
    self.view.reload()

This will actually cause the library to completely reread the contents of the PDF file.

gvl
  • 903
  • 7
  • 16