Skip to content

Demo: GUI script to display a PDF using wxPython or Tkinter

Jorj X. McKie edited this page Apr 3, 2017 · 8 revisions

PDFdisplay.py

This demonstrates the superior rendering capabilities of MuPDF using wxPython as the display manager.
To access the program, have a look at PDFdisplay.py in the demo folder of the repository.

Changes in PyMuPDF v1.9.2

The program now supports the display of links contained in a page. When activating this option, the next displayed page will mark any link with a blue-lined frame. When clicking such a link, the respective action will be performed. Supported are links within the document (GoTo), links to other documents (GoToR, in this case subprocess.Popen is used to display the requested document with the standard PDF reader), links to web sites (URI) and named destinations. This is how the new version will look like:

Adapt for Tkinter

This demo can easily be adopted to Tkinter. You need the imports

from Tkinter import Tk, Canvas, Frame, BOTH, NW
from PIL import Image, ImageTk  

and do the following to display each PDF page image:

#-----------------------------------------------------------------
# MuPDF code
#-----------------------------------------------------------------
pix = doc.getPagePixmap(pno - 1)      # create pixmap for a page

#-----------------------------------------------------------------
# Tkinter code
#-----------------------------------------------------------------
self.img = Image.frombytes("RGBA", [pix.width, pix.height], pix.samples)
selfphoto = ImageTk.PhotoImage(self.img)
canvas = Canvas(self, width=self.img.size[0]+20, 
                height=self.img.size[1]+20)
canvas.create_image(10, 10, anchor=NW, image=self.photo)
canvas.pack(fill=BOTH, expand=1)

Using wxPython-Phoenix

When using this newest wxPython version, you can simply use its new pdfviewer class. Here is the sample code provided in the online documentation.

Clone this wiki locally