Phoenix Logo

phoenix_title wx.lib.pdfviewer

pdfViewer class is derived from wx.ScrolledWindow class and can display and print PDF files.

Description

The pdfViewer class is derived from wx.ScrolledWindow and can display and print PDF files. The whole file can be scrolled from end to end at whatever magnification (zoom-level) is specified.

The viewer uses PyMuPDF (version 1.9.2 or later) or PyPDF2. If neither of them are installed an import error exception will be raised.

PyMuPDF contains the Python bindings for the underlying MuPDF library, a cross platform, complete PDF rendering library that is GPL licenced.

Further details on PyMuPDF can be found via http://pythonhosted.org/PyMuPDF

PyPDF2 provides a PdfFileReader class that is used to read the content stream of a PDF file which is subsequently rendered by pdfViewer itself. Please note that this is not a complete implementation of the pdf specification and will probably fail to display any random file you supply. However it does seem to satisfactorily render files typically produced by ReportLab using Western languages. The main limitation is that it doesn’t currently support embedded fonts.

Additional details on PyPDF2 can be found via http://pythonhosted.org/PyPDF2

There is an optional pdfButtonPanel class, derived from buttonpanel, that can be placed, for example, at the top of the scrolled viewer window, and which contains navigation and zoom controls.

Usage

Sample usage:

import wx
import wx.lib.sized_controls as sc

from wx.lib.pdfviewer import pdfViewer, pdfButtonPanel

class PDFViewer(sc.SizedFrame):
    def __init__(self, parent, **kwds):
        super(PDFViewer, self).__init__(parent, **kwds)

        paneCont = self.GetContentsPane()
        self.buttonpanel = pdfButtonPanel(paneCont, wx.NewId(),
                                wx.DefaultPosition, wx.DefaultSize, 0)
        self.buttonpanel.SetSizerProps(expand=True)
        self.viewer = pdfViewer(paneCont, wx.NewId(), wx.DefaultPosition,
                                wx.DefaultSize,
                                wx.HSCROLL|wx.VSCROLL|wx.SUNKEN_BORDER)
        self.viewer.UsePrintDirect = ``False``
        self.viewer.SetSizerProps(expand=True, proportion=1)

        # introduce buttonpanel and viewer to each other
        self.buttonpanel.viewer = self.viewer
        self.viewer.buttonpanel = self.buttonpanel


if __name__ == '__main__':
    import wx.lib.mixins.inspection as WIT
    app = WIT.InspectableApp(redirect=False)


    pdfV = PDFViewer(None, size=(800, 600))
    pdfV.viewer.UsePrintDirect = ``False``
    pdfV.viewer.LoadFile(r'a path to a .pdf file')
    pdfV.Show()

    app.MainLoop()

Alternatively you can drive the viewer from controls in your own application.

Externally callable methods are:

LoadFile

Save

Print

SetZoom

GoPage

The viewer renders the pdf file content using Cairo if installed, otherwise wx.GraphicsContext is used. Printing is achieved by writing directly to a wx.PrinterDC and using wx.Printer.

The icons used in pdfButtonPanel are Free Icons by Axialis Software: http://www.axialis.com. You can freely use them in any project, commercially or not, but you must keep the credits of the authors: “Axialis Team”, even if you modify them. See ./bitmaps/ReadMe.txt for further details.

module_summary Modules Summary

buttonpanel This module provides the pdfButtonPanel
images  
viewer This module provides the pdfViewer to view PDF