Printing framework - Part 6 (Phoenix)

Keywords : PDFViewer, PyMuPDF, Printing, Reading, PDF.


Demonstrating :

Tested py3.x, wx4.x and Win10.

Printing is an essential element for your programs, here we show you how to print.

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


PDFViewer (David Hughes)

The wx.lib.pdfviewer.pdfViewer class can display and print PDF files.

You must install Python bindings for the PDF rendering library MuPDF before use it :

pip install PyMuPDF

img_sample_one.png

   1 # sample_one.py
   2 
   3 import os
   4 import sys
   5 import wx
   6 
   7 try:
   8     from wx.lib.pdfviewer import pdfViewer, pdfButtonPanel
   9     havePyPdf = True
  10 except ImportError:
  11     havePyPdf = False
  12 
  13 # class My_Panel
  14 # class My_Frame
  15 # class My_App
  16 
  17 #-------------------------------------------------------------------------------
  18 
  19 class My_Panel(wx.Panel):
  20     """
  21     Create a panel for my frame.
  22     """
  23     def __init__(self, parent):
  24         wx.Panel.__init__(self, parent, -1)
  25 
  26         hsizer = wx.BoxSizer(wx.HORIZONTAL)
  27         vsizer = wx.BoxSizer(wx.VERTICAL)
  28 
  29         self.buttonpanel = pdfButtonPanel(self,
  30                                           wx.ID_ANY,
  31                                           wx.DefaultPosition,
  32                                           wx.DefaultSize,
  33                                           0)
  34 
  35         vsizer.Add(self.buttonpanel, 0,
  36                    wx.GROW|wx.LEFT|wx.RIGHT|wx.TOP, 5)
  37 
  38         self.viewer = pdfViewer(self,
  39                                 wx.ID_ANY,
  40                                 wx.DefaultPosition,
  41                                 wx.DefaultSize,
  42                                 wx.HSCROLL|wx.VSCROLL|
  43                                 wx.SUNKEN_BORDER)
  44 
  45         vsizer.Add(self.viewer, 1,
  46                    wx.GROW|wx.LEFT|wx.RIGHT|wx.BOTTOM, 5)
  47 
  48         loadbutton = wx.Button(self,
  49                                wx.ID_ANY,
  50                                "Load PDF file",
  51                                wx.DefaultPosition,
  52                                wx.DefaultSize,
  53                                0)
  54 
  55         vsizer.Add(loadbutton, 0,
  56                    wx.ALIGN_CENTER|wx.ALL, 5)
  57 
  58         hsizer.Add(vsizer, 1,
  59                    wx.GROW|wx.ALL, 5)
  60 
  61         self.SetSizer(hsizer)
  62         self.SetAutoLayout(True)
  63 
  64         # Introduce buttonpanel and viewer to each other.
  65         self.buttonpanel.viewer = self.viewer
  66         self.viewer.buttonpanel = self.buttonpanel
  67 
  68         self.Bind(wx.EVT_BUTTON, self.OnLoadButton, loadbutton)
  69 
  70     #---------------------------------------------------------------------------
  71 
  72     def OnLoadButton(self, event):
  73         """
  74         ...
  75         """
  76 
  77         dlg = wx.FileDialog(self, wildcard="*.pdf")
  78 
  79         if dlg.ShowModal() == wx.ID_OK:
  80             wx.BeginBusyCursor()
  81             self.viewer.LoadFile(dlg.GetPath())
  82             wx.EndBusyCursor()
  83 
  84         dlg.Destroy()
  85 
  86 #-------------------------------------------------------------------------------
  87 
  88 class My_Frame(wx.Frame):
  89     """
  90     Create a main frame for my application.
  91     """
  92     def __init__(self):
  93         style = (wx.DEFAULT_FRAME_STYLE)
  94         wx.Frame.__init__(self, None, -1,
  95                           title="PDFViewer (PyMuPDF)",
  96                           size=(700, 500),
  97                           style=style)
  98 
  99         #------------
 100 
 101         frameicon = wx.Icon("Icons/wxWidgets.ico")
 102         self.SetIcon(frameicon)
 103 
 104         #------------
 105 
 106         pnl = My_Panel(self)
 107 
 108         #------------
 109 
 110         self.CenterOnScreen(wx.BOTH)
 111 
 112         #------------
 113 
 114         self.Show(True)
 115 
 116 #-------------------------------------------------------------------------------
 117 
 118 class My_App(wx.App):
 119     """
 120     ...
 121     """
 122     def OnInit(self):
 123 
 124         #------------
 125 
 126         self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
 127 
 128         #------------
 129 
 130         frame = My_Frame()
 131         self.SetTopWindow(frame)
 132         frame.Show(True)
 133 
 134         return True
 135 
 136 #-------------------------------------------------------------------------------
 137 
 138 def main():
 139     app = My_App(False)
 140     app.MainLoop()
 141 
 142 #-------------------------------------------------------------------------------
 143 
 144 if __name__ == "__main__" :
 145     main()

Available with wxPython demo (Miscellaneous/PDFViewer).


Download source

source.zip


Additional Information

Link :

https://pypi.org/project/PyMuPDF/

https://wxpython.org/Phoenix/docs/html/wx.lib.pdfviewer.html

https://github.com/pymupdf/PyMuPDF/wiki/GUI-scripts-to-display-a-PDF-using-wxPython-or-Tkinter

https://wiki.wxpython.org/MoreCommentsOnPrinting

https://wiki.wxpython.org/PrintingWithReportGenerators

http://www.blog.pythonlibrary.org/2012/06/17/an-intro-to-rst2pdf-changing-restructured-text-into-pdfs-with-python/

http://www.blog.pythonlibrary.org/2010/05/15/manipulating-pdfs-with-python-and-pypdf/

https://www.blog.pythonlibrary.org/2010/02/14/python-windows-and-printers/

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

David Hughes (PDFViewer).


About this page

Date (d/m/y) Person (bot) Comments :

10/10/18 - Ecco (Created page for wxPython Phoenix).


Comments

- blah, blah, blah...

Printing framework - Part 6 (Phoenix) (last edited 2020-10-07 10:39:54 by Ecco)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.