def CreateDoc(self, docName, pageSize, isPortrait=True): print("Create", docName, "Document") # handle = win32print.OpenPrinter(self.printer_name ) self.SetPrinter(self.printer_name, isPortrait) self.PageSize = (int(2970 - pageSize[0]), int(1050 - pageSize[1] / 2)) self.Document_Name = docName # self.dc = win32ui.CreateDC() # self.dc.CreatePrinterDC(self.printer_name) # https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-setmapmode self.dc.SetMapMode(win32con.MM_LOMETRIC) self.dc.StartDoc(docName) self.dc.SetBkMode(win32con.TRANSPARENT) self.dc.SelectObject(self.font) self.pen = win32ui.CreatePen(0, 0, 0)
def OnInitialUpdate(self): ret = self._obj_.OnInitialUpdate() self.colors = { 'Black': (0x00 << 0) + (0x00 << 8) + (0x00 << 16), 'Red': (0xff << 0) + (0x00 << 8) + (0x00 << 16), 'Green': (0x00 << 0) + (0xff << 8) + (0x00 << 16), 'Blue': (0x00 << 0) + (0x00 << 8) + (0xff << 16), 'Cyan': (0x00 << 0) + (0xff << 8) + (0xff << 16), 'Magenta': (0xff << 0) + (0x00 << 8) + (0xff << 16), 'Yellow': (0xff << 0) + (0xff << 8) + (0x00 << 16), } self.pens = {} for name, color in self.colors.iteritems(): self.pens[name] = win32ui.CreatePen(win32con.PS_SOLID, 5, color) self.pen = None self.size = (128, 128) self.SetScaleToFitSize(self.size) self.HookCommand(self.OnFilePrint, afxres.ID_FILE_PRINT) self.HookCommand(self.OnFilePrintPreview, win32ui.ID_FILE_PRINT_PREVIEW) return ret
def OnInitialUpdate(self): ret = self._obj_.OnInitialUpdate() self.colors = { "Black": (0x00 << 0) + (0x00 << 8) + (0x00 << 16), "Red": (0xFF << 0) + (0x00 << 8) + (0x00 << 16), "Green": (0x00 << 0) + (0xFF << 8) + (0x00 << 16), "Blue": (0x00 << 0) + (0x00 << 8) + (0xFF << 16), "Cyan": (0x00 << 0) + (0xFF << 8) + (0xFF << 16), "Magenta": (0xFF << 0) + (0x00 << 8) + (0xFF << 16), "Yellow": (0xFF << 0) + (0xFF << 8) + (0x00 << 16), } self.pens = {} for name, color in self.colors.items(): self.pens[name] = win32ui.CreatePen(win32con.PS_SOLID, 5, color) self.pen = None self.size = (128, 128) self.SetScaleToFitSize(self.size) self.HookCommand(self.OnFilePrint, afxres.ID_FILE_PRINT) self.HookCommand(self.OnFilePrintPreview, win32ui.ID_FILE_PRINT_PREVIEW) return ret
def begin_document(self, desc = "MSWinPrint.py print job"): # open the printer if self.printer is None: self.printer = win32print.GetDefaultPrinter() self.hprinter = win32print.OpenPrinter(self.printer) # load default settings devmode = win32print.GetPrinter(self.hprinter, 8)["pDevMode"] # change paper size and orientation if self.papersize is not None: if type(self.papersize) is int: devmode.PaperSize = self.papersize else: devmode.PaperSize = paper_sizes[self.papersize] if self.orientation is not None: devmode.Orientation = orientations[self.orientation] if self.duplex is not None: devmode.Duplex = duplexes[self.duplex] # create dc using new settings self.hdc = win32gui.CreateDC("WINSPOOL", self.printer, devmode) self.dc = win32ui.CreateDCFromHandle(self.hdc) # self.dc = win32ui.CreateDC() # if self.printer is not None: # self.dc.CreatePrinterDC(self.printer) # else: # self.dc.CreatePrinterDC() self.dc.SetMapMode(win32con.MM_TWIPS) # hundredths of inches self.dc.StartDoc(desc) self.pen = win32ui.CreatePen(0, int(scale_factor), 0) self.dc.SelectObject(self.pen) self.page = 1
def SetLineWidth(self, w=2): pen = win32ui.CreatePen(win32con.PS_SOLID, w, 0) self.dc.SelectObject(pen)
def test_ps(printer='MyPSPrinter', filename=r'G:/test.ps', margin=(0.25,1.5,0.25,1.0), font_size=24, text=None): '''render postscript text and graphics to a file''' if text is None: text_data = "This is a test.\nThis is only a test." else: text_data = (text.encode()) # Get the printer's DEVMODE structure h_printer = win32print.OpenPrinter(printer) devmode = win32print.GetPrinter(h_printer, 2)['pDevMode'] win32print.ClosePrinter(h_printer) # set up the device context # see MSDN: ff552837, aa452943, dd319099, dd145045 devmode.FormName = 'Letter' # or 'A4' devmode.PaperSize = win32con.DMPAPER_LETTER # or DMPAPER_A4 devmode.Orientation = win32con.DMORIENT_PORTRAIT devmode.PrintQuality = win32con.DMRES_HIGH devmode.Color = win32con.DMCOLOR_MONOCHROME devmode.TTOption = win32con.DMTT_SUBDEV devmode.Scale = 100 devmode.Fields |= (win32con.DM_FORMNAME | win32con.DM_PAPERSIZE | win32con.DM_ORIENTATION | win32con.DM_PRINTQUALITY | win32con.DM_COLOR | win32con.DM_TTOPTION | win32con.DM_SCALE) h_dc = win32gui.CreateDC('WINSPOOL', printer, devmode) dc = win32ui.CreateDCFromHandle(h_dc) dc.SetMapMode(win32con.MM_TWIPS) # or MM_HIMETRIC (0.01 mm) # begin writing the document dc.StartDoc('Postscript File Print', filename) dc.StartPage() # we need a pen and a font scale = 20 # 72 pt/inch * 20 twip/pt = 1440 inch = 72*scale pen = win32ui.CreatePen(win32con.PS_SOLID, scale, # 1 pt 0) # black dc.SelectObject(pen) font = win32ui.CreateFont({ 'name': 'Times New Roman', 'height': font_size * scale, 'weight': win32con.FW_NORMAL}) dc.SelectObject(font) # output the text x = int(margin[0] * inch) y = -int(margin[1] * inch) width = int((8.5 - margin[0] - margin[2]) * inch) height = int((11.0 - margin[1] - margin[3]) * inch) rect = (x, y, x + width, y - height) dc.DrawText(text_data, rect, win32con.DT_LEFT) if text is None: # draw 8 steps starting at x = 0.25", y = 3" width = inch height = inch for n in range(8): x = n * width + 18*scale y = -n * height - 3*inch dc.MoveTo((x, y)) dc.LineTo((x + width, y)) dc.MoveTo((x + width, y)) dc.LineTo((x + width, y - height)) dc.EndPage() dc.EndDoc()
def Print(self, order_id, data): file_name = order_id + '.txt' open(file_name, 'w').write(data) scale_factor = 20 font_heading = win32ui.CreateFont({ "name": "Arial Unicode MS", # a font name "height": int(scale_factor * 4), # 10 pt "weight": 500, # 400 = normal }) font = win32ui.CreateFont({ "name": "Arial Unicode MS", # a font name "height": int(scale_factor * 2), # 10 pt "weight": 500, # 400 = normal }) font_afterline = win32ui.CreateFont({ "name": "Arial Unicode MS", # a font name "height": int(scale_factor * 14), # 10 pt "weight": 500, # 400 = normal }) font_detail = win32ui.CreateFont({ "name": "Arial Unicode MS", # a font name "height": int(scale_factor * 1.5), # 10 pt "weight": 500, # 400 = normal }) X_Heading = 60 Y_Heading = 150 X_ID = 150 Y_ID = 225 X_Contact = 140 Y_Contact = 280 X_DateTime = 130 Y_DateTime = 330 X_End = 50 Y_End = 550 X_Details = 100 Y_Details = -2200 X_Details1 = 100 Y_Details1 = -2500 X_Details2 = 100 Y_Details2 = -2800 X_Details3 = 100 Y_Details3 = -3100 X_Total = 1200 Y_Total = -800 hDC = win32ui.CreateDC() hDC.CreatePrinterDC(win32print.GetDefaultPrinter()) hDC.StartDoc("Order#" + order_id) hDC.SelectObject(font_heading) hDC.StartPage() hDC.TextOut(X_Heading, Y_Heading, "PetroFDS") hDC.SelectObject(font) hDC.TextOut(X_ID, Y_ID, "Order Receipt# " + order_id) hDC.SelectObject(font_detail) hDC.TextOut(X_Contact, Y_Contact, "Contact Us At: +12345678910") hDC.TextOut(X_DateTime, Y_DateTime, "Date & Time: " + time.strftime("%c")) hDC.SetMapMode(win32con.MM_TWIPS) pen = win32ui.CreatePen(win32con.PS_DASH, 20, RGB(148, 37, 37)) hDC.SetBkMode(win32con.TRANSPARENT) hDC.SelectObject(pen) hDC.MoveTo(-2000, -2800) hDC.LineTo(4000, -2800) hDC.SelectObject(font_afterline) x = 1 db = Database() cursor = db.conn.cursor() sql = """ SELECT id, order_detail_id, user_id, quantity, price, name, (SELECT sum(price) FROM order_invoice WHERE order_detail_id='""" + order_id + """') total FROM order_invoice WHERE order_detail_id='""" + order_id + """' """ cursor.execute(sql) rows = cursor.fetchall() for row in rows: totals = row[6] y = 10000 * x x = x + 1 dr = open(file_name, 'r').read() hDC.DrawText(dr, (150, -3000, y, -y), win32con.DT_LEFT) hDC.EndPage() hDC.EndDoc()