How to use printing framework - Part 7 (Phoenix)

Keywords : PDFWindow, Adobe Acrobat Reader, Viewer, 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 ! (!)


PDFWindow (only Windows)

Read PDF files (.pdf) with wxPython using wx.lib.pdfwin.PDFWindow class ActiveX control.

You must install this package for use it :

pip install comtypes

PDF file : document.pdf

   1 # sample_one.py
   2 
   3 """
   4 
   5 Read PDF files (.pdf) with wxPython
   6 using wx.lib.pdfwin.PDFWindow class ActiveX control
   7 from wxPython's new wx.activex module, this allows one
   8 to use an ActiveX control, as if it would be wx.Window.
   9 it embeds the Adobe Acrobat Reader.
  10 This works only with Windows.
  11 
  12 """
  13 
  14 import os
  15 import sys
  16 import wx
  17 
  18 if wx.Platform == "__WXMSW__":
  19     from wx.lib.pdfwin import PDFWindow
  20 else:
  21     dlg = wx.MessageDialog(self,
  22                            message="This demo only works on Microsoft Windows.",
  23                            caption="Sorry",
  24                            style=wx.OK|
  25                                  wx.ICON_WARNING)
  26     dlg.ShowModal()
  27     dlg.Destroy()
  28 
  29 appDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
  30 doc = os.path.join(appDir, "document.pdf")
  31 
  32 # class My_Frame
  33 # class My_App
  34 
  35 #-------------------------------------------------------------------------------
  36 
  37 class My_Frame(wx.Frame):
  38     def __init__(self, parent, id, title):
  39         wx.Frame.__init__(self, parent, -1,
  40                           title, size=(720, 600),
  41                           style=wx.DEFAULT_FRAME_STYLE |
  42                                 wx.NO_FULL_REPAINT_ON_RESIZE)
  43 
  44         #------------
  45 
  46         frameicon = wx.Icon("Icons/wxWidgets.ico")
  47         self.SetIcon(frameicon)
  48 
  49         #------------
  50 
  51         # Simplified init method.
  52         self.CreateCtrls()
  53         self.LoadPdf()
  54         self.BindEvents()
  55         self.DoLayout()
  56 
  57         #---------------------------------------------------------------
  58 
  59         self.CenterOnScreen()
  60 
  61     #-------------------------------------------------------------------
  62 
  63     def CreateCtrls(self):
  64         """
  65         ...
  66         """
  67 
  68         fontSize = self.GetFont().GetPointSize()
  69         boldFont = wx.Font(fontSize, wx.SWISS, wx.NORMAL, wx.BOLD)
  70 
  71         #------------
  72 
  73         self.panel = wx.Panel(self, -1)
  74 
  75         #------------
  76 
  77         # Add PDFWindow
  78         self.pdf = PDFWindow(self.panel, style=wx.SUNKEN_BORDER)
  79         self.pdf.setView("FitV")
  80         self.pdf.setPageMode("Bookmarks")
  81         self.pdf.setLayoutMode("SinglePage")
  82         self.pdf.setShowScrollbars(True)
  83 
  84         #------------
  85 
  86         # Add controls
  87         self.btn1 = wx.Button(self.panel, -1, "&Open")
  88         self.btn1.SetToolTip("Open PDF file.")
  89 
  90         self.btn2 = wx.Button(self.panel,  -1, "<<", size=(30, -1))
  91         self.btn2.SetToolTip("First page.")
  92         self.btn2.SetFont(boldFont)
  93 
  94         self.btn3 = wx.Button(self.panel, -1, "<", size=(30, -1))
  95         self.btn3.SetToolTip("Previous page.")
  96         self.btn3.SetFont(boldFont)
  97 
  98         self.btn4 = wx.Button(self.panel, -1, ">", size=(30, -1))
  99         self.btn4.SetToolTip("Next page.")
 100         self.btn4.SetFont(boldFont)
 101 
 102         self.btn5 = wx.Button(self.panel,  -1, ">>", size=(30, -1))
 103         self.btn5.SetToolTip("Last page.")
 104         self.btn5.SetFont(boldFont)
 105 
 106         self.txt1 = wx.StaticText(self.panel, -1, "   Go to page" )
 107         self.txtCtrl = wx.TextCtrl(self.panel, -1, "0", size=[30, -1])
 108 
 109         self.txt2 = wx.StaticText(self.panel, -1, "     Zoom")
 110         self.zoom = wx.Choice(self.panel, -1,
 111                               choices=["Default",
 112                                        "Fit",
 113                                        "FitH",
 114                                        "FitV",
 115                                        "25%",
 116                                        "50%",
 117                                        "75%",
 118                                        "100%",
 119                                        "125%",
 120                                        "200%",
 121                                        "400%"])
 122         self.zoom.SetSelection(0)
 123         self.zoom.SetToolTip("Zoom.")
 124 
 125         self.btn6 = wx.Button(self.panel, -1, "&Pr&int")
 126         self.btn6.SetToolTip("Print PDF file.")
 127 
 128         self.btn7 = wx.Button(self.panel, -1, "&Quit")
 129         self.btn7.SetToolTip("Quit application.")
 130 
 131 
 132     def BindEvents(self):
 133         """
 134         Bind all the events related to my frame.
 135         """
 136 
 137         self.Bind(wx.EVT_BUTTON, self.OnOpenBtn, self.btn1)
 138         self.Bind(wx.EVT_BUTTON, self.OnFirstPageBtn, self.btn2)
 139         self.Bind(wx.EVT_BUTTON, self.OnPrevPageBtn, self.btn3)
 140         self.Bind(wx.EVT_BUTTON, self.OnNextPageBtn, self.btn4)
 141         self.Bind(wx.EVT_BUTTON, self.OnLastPageBtn, self.btn5)
 142         self.Bind(wx.EVT_BUTTON, self.OnPrintBtn, self.btn6)
 143         self.Bind(wx.EVT_BUTTON, self.OnCloseBtn, self.btn7)
 144 
 145         self.Bind(wx.EVT_TEXT, self.OnGotoPage, self.txtCtrl)
 146         self.Bind(wx.EVT_CHOICE, self.OnZoom, self.zoom)
 147 
 148         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
 149         self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
 150 
 151 
 152     def DoLayout(self):
 153         """
 154         Manage widgets Layout.
 155         """
 156 
 157         # MainSizer is the top-level one that manages everything.
 158         sizer = wx.BoxSizer(wx.VERTICAL)
 159         sizer.Add(self.pdf, proportion=1, flag=wx.EXPAND)
 160 
 161         #------------
 162 
 163         btnSizer = wx.BoxSizer(wx.HORIZONTAL)
 164         btnSizer.Add(self.btn1, proportion=0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
 165         btnSizer.Add(self.btn2, proportion=0, flag=wx.ALIGN_CENTER|wx.ALL, border=2)
 166         btnSizer.Add(self.btn3, proportion=0, flag=wx.ALIGN_CENTER|wx.ALL, border=2)
 167         btnSizer.Add(self.btn4, proportion=0, flag=wx.ALIGN_CENTER|wx.ALL, border=2)
 168         btnSizer.Add(self.btn5, proportion=0, flag=wx.ALIGN_CENTER|wx.ALL, border=2)
 169         btnSizer.Add(self.txt1, proportion=0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
 170         btnSizer.Add(self.txtCtrl, proportion=0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
 171         btnSizer.Add(self.txt2, proportion=0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
 172         btnSizer.Add(self.zoom, proportion=0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
 173         btnSizer.Add(self.btn6, proportion=0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
 174         btnSizer.Add(self.btn7, proportion=0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
 175         btnSizer.Add((50,-1), proportion=2, flag=wx.EXPAND)
 176 
 177         #------------
 178 
 179         sizer.Add(btnSizer, proportion=0, flag=wx.EXPAND)
 180 
 181         #------------
 182 
 183         # Finally, tell the panel to use the mainSizer for layout.
 184         self.panel.SetSizer(sizer)
 185         self.panel.SetAutoLayout(True)
 186 
 187 
 188     def LoadPdf(self):
 189         """
 190         ...
 191         """
 192 
 193         self.pdf.LoadFile(doc)
 194 
 195 
 196     def OnOpenBtn(self, event):
 197         """
 198         ...
 199         """
 200 
 201         dlg = wx.FileDialog(self, wildcard="*.pdf")
 202 
 203         if dlg.ShowModal() == wx.ID_OK:
 204             wx.BeginBusyCursor()
 205             self.pdf.LoadFile(dlg.GetPath())
 206             wx.EndBusyCursor()
 207 
 208         dlg.Destroy()
 209 
 210 
 211     def OnFirstPageBtn(self, event):
 212         """
 213         ...
 214         """
 215 
 216         self.pdf.gotoFirstPage()
 217 
 218 
 219     def OnPrevPageBtn(self, event):
 220         """
 221         ...
 222         """
 223 
 224         self.pdf.gotoPreviousPage()
 225 
 226 
 227     def OnNextPageBtn(self, event):
 228         """
 229         ...
 230         """
 231 
 232         self.pdf.gotoNextPage()
 233 
 234 
 235     def OnLastPageBtn(self, event):
 236         """
 237         ...
 238         """
 239 
 240         self.pdf.gotoLastPage()
 241 
 242 
 243     def OnGotoPage(self, event):
 244         """
 245         ...
 246         """
 247 
 248         npage = event.GetEventObject().GetValue()
 249 
 250         try:
 251             self.pdf.setCurrentPage(int(npage))
 252         except ValueError:
 253             pass
 254 
 255 
 256     def OnZoom(self, event):
 257         """
 258         ...
 259         """
 260 
 261         astring = event.GetEventObject().GetStringSelection()
 262 
 263         if astring.startswith('Fit'):
 264             self.pdf.setView(astring)
 265         else:
 266             try:
 267                 percent = float(astring.replace('%',''))
 268                 self.pdf.setZoom(percent)
 269             except ValueError:
 270                 pass
 271 
 272 
 273     def OnPrintBtn(self, event):
 274         """
 275         ...
 276         """
 277 
 278         self.pdf.Print()
 279 
 280 
 281     def OnCloseBtn(self, event):
 282         """
 283         ...
 284         """
 285 
 286         self.Close(True)
 287 
 288 
 289     def OnEraseBackground(self, event):
 290         """
 291         ...
 292         """
 293 
 294         dc = event.GetDC()
 295 
 296         if not dc:
 297             dc = wx.ClientDC(self)
 298             rect = self.GetUpdateRegion().GetBox()
 299             dc.SetClippingRect(rect)
 300 
 301 
 302     def OnCloseWindow(self, event):
 303         """
 304         ...
 305         """
 306 
 307         self.Destroy()
 308 
 309 #-------------------------------------------------------------------------------
 310 
 311 class My_App(wx.App):
 312     """
 313     ...
 314     """
 315     def OnInit(self):
 316 
 317         #------------
 318 
 319         self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
 320 
 321         #------------
 322 
 323         frame = My_Frame(None, -1, "PDFWindow (ActiveX)")
 324         self.SetTopWindow(frame)
 325         frame.Show(True)
 326 
 327         return True
 328 
 329 #-------------------------------------------------------------------------------
 330 
 331 def main():
 332     app = My_App(False)
 333     app.MainLoop()
 334 
 335 #-------------------------------------------------------------------------------
 336 
 337 if __name__ == "__main__" :
 338     main()

Available with wxPython demo (More Windows/Controls/ActiveX_PDFWindow).


Download source

source.zip


Additional Information

Link :

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

Robin Dunn.


About this page

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

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


Comments

- blah, blah, blah...

How to use printing framework - Part 7 (Phoenix) (last edited 2020-12-13 17:54:24 by Ecco)

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