| 1 |
## Printer.py
|
| 2 |
## Printing frame work
|
| 3 |
|
| 4 |
#from wxPython.wx import wxPrintout, wxPrintData, wxPAPER_LETTER, wxPrintDialogData
|
| 5 |
#from wxPython.wx import wxPrinter, wxMessageBox, wxPrintPreview, wxPrintDialog
|
| 6 |
from wxPython.wx import *
|
| 7 |
|
| 8 |
def GetErrorText():
|
| 9 |
"Put your error text logic here. See Python Cookbook for a useful example of error text."
|
| 10 |
return "Some error occurred."
|
| 11 |
|
| 12 |
class Printer(wxPrintout):
|
| 13 |
def __init__(self, frame):
|
| 14 |
"Prepares the Printing object. Note: change current_y for 1, 1.5, 2 spacing for lines. "
|
| 15 |
wxPrintout.__init__(self)
|
| 16 |
self.printer_config = wxPrintData()
|
| 17 |
self.printer_config.SetPaperId(wxPAPER_LETTER)
|
| 18 |
self.frame = frame
|
| 19 |
self.doc_text = ''
|
| 20 |
self.doc_name = ''
|
| 21 |
self.current_y = 15 #y should be either (15, 22, 30)
|
| 22 |
if self.current_y == 15:
|
| 23 |
self.num_lines_per_page = 50
|
| 24 |
elif self.current_y == 22:
|
| 25 |
self.num_lines_per_page = 35
|
| 26 |
else:
|
| 27 |
self.num_lines_per_page = 25
|
| 28 |
|
| 29 |
|
| 30 |
def Print(self, text, doc_name):
|
| 31 |
"Prints the given text. Currently doc_name logic doesn't exist. E.g. might be useful for a footer.."
|
| 32 |
self.doc_text = text
|
| 33 |
self.doc_name = doc_name
|
| 34 |
pdd = wxPrintDialogData()
|
| 35 |
pdd.SetPrintData(self.printer_config)
|
| 36 |
printer = wxPrinter(pdd)
|
| 37 |
## if not printer.Print(self.frame,self):
|
| 38 |
## wxMessageBox("Unable to print the document.")
|
| 39 |
## else:
|
| 40 |
self.printer_config = printer.GetPrintDialogData().GetPrintData()
|
| 41 |
|
| 42 |
def PreviewText(self, text, doc_name):
|
| 43 |
"This function displays the preview window for the text with the given header."
|
| 44 |
try:
|
| 45 |
self.doc_name = doc_name
|
| 46 |
self.doc_text = text
|
| 47 |
|
| 48 |
#Destructor fix by Peter Milliken --
|
| 49 |
print1 = Printer(self.frame, text = self.doc_text)
|
| 50 |
print2 = Printer(self.frame, text = self.doc_text)
|
| 51 |
preview = wxPrintPreview(print1, print2, self.printer_config)
|
| 52 |
#preview = wxPrintPreview(self,self,self.printer_config)
|
| 53 |
if not preview.Ok():
|
| 54 |
wxMessageBox("Unable to display preview of document.")
|
| 55 |
return
|
| 56 |
|
| 57 |
preview_window = PreviewFrame(preview, self.frame, "Print Preview - %s" % doc_name)
|
| 58 |
preview_window.Initialize()
|
| 59 |
preview_window.SetPosition(self.frame.GetPosition())
|
| 60 |
preview_window.SetSize(self.frame.GetSize())
|
| 61 |
preview_window.MakeModal(TRUE)
|
| 62 |
preview_window.Show(TRUE)
|
| 63 |
except:
|
| 64 |
wxMessageBox(GetErrorText())
|
| 65 |
|
| 66 |
def PageSetup(self):
|
| 67 |
"This function handles displaying the Page Setup window and retrieving the user selected options."
|
| 68 |
config_dialog = wxPrintDialog(self.frame)
|
| 69 |
config_dialog.GetPrintDialogData().SetPrintData(self.printer_config)
|
| 70 |
config_dialog.GetPrintDialogData().SetSetupDialog(TRUE)
|
| 71 |
config_dialog.ShowModal()
|
| 72 |
self.printer_config = config_dialog.GetPrintDialogData().GetPrintData()
|
| 73 |
config_dialog.Destroy()
|
| 74 |
|
| 75 |
def OnBeginDocument(self,start,end):
|
| 76 |
"Do any end of document logic here."
|
| 77 |
return self.base_OnBeginDocument(start,end)
|
| 78 |
|
| 79 |
def OnEndDocument(self):
|
| 80 |
"Do any end of document logic here."
|
| 81 |
self.base_OnEndDocument()
|
| 82 |
|
| 83 |
def OnBeginPrinting(self):
|
| 84 |
"Do printing initialization logic here."
|
| 85 |
self.base_OnBeginPrinting()
|
| 86 |
|
| 87 |
def OnEndPrinting(self):
|
| 88 |
"Do any post printing logic here."
|
| 89 |
self.base_OnEndPrinting()
|
| 90 |
|
| 91 |
def OnPreparePrinting(self):
|
| 92 |
"Do any logic to prepare for printing here."
|
| 93 |
self.base_OnPreparePrinting()
|
| 94 |
|
| 95 |
def HasPage(self, page_num):
|
| 96 |
"This function is called to determine if the specified page exists."
|
| 97 |
return len(self.GetPageText(page_num)) > 0
|
| 98 |
|
| 99 |
def GetPageInfo(self):
|
| 100 |
"""
|
| 101 |
This returns the page information: what is the page range available, and what is the selected page range.
|
| 102 |
Currently the selected page range is always the available page range. This logic should be changed if you need
|
| 103 |
greater flexibility.
|
| 104 |
"""
|
| 105 |
|
| 106 |
minPage = 1
|
| 107 |
maxPage = int(len(self.doc_text.split('\n'))/self.num_lines_per_page)
|
| 108 |
fromPage, toPage = minPage, maxPage
|
| 109 |
return (minPage,maxPage,fromPage,toPage)
|
| 110 |
|
| 111 |
def OnPrintPage(self, page_num):
|
| 112 |
"This function / event is executed for each page that needs to be printed."
|
| 113 |
dc = self.GetDC()
|
| 114 |
x,y = 25, self.current_y
|
| 115 |
if not self.IsPreview():
|
| 116 |
y *=4
|
| 117 |
line_count = 1
|
| 118 |
for line in self.GetPageText(page_num):
|
| 119 |
dc.DrawText(line, x, y*line_count)
|
| 120 |
line_count += 1
|
| 121 |
|
| 122 |
return TRUE
|
| 123 |
|
| 124 |
def GetPageText(self, page_num):
|
| 125 |
"This function returns the text to be displayed for the given page number."
|
| 126 |
lines = self.doc_text.split('\n')
|
| 127 |
lines_for_page = lines[(page_num -1)*self.num_lines_per_page: page_num*(self.num_lines_per_page-1)]
|
| 128 |
return lines_for_page |