def __init__(self): self.hDC = win32ui.CreateDC() print("Pritner Helper")
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()
def print_image(image_url): file_name = urlsplit(image_url).path.split('/')[-1] img_dir_path = os.path.join(os.path.dirname(__file__), TMP_IMG_DIR) tmp_img_path = os.path.join(img_dir_path, file_name) if not os.path.exists(img_dir_path): os.makedirs(img_dir_path) # Another way to get file from url: # import urllib # resource = urllib.urlopen(image_url) # output = open(tmp_img_path,"wb") # output.write(resource.read()) # output.close() logger.debug('Trying to fetch image from url: {}'.format(image_url)) try: img_request = requests.get(image_url, stream=True) except ConnectionError as e: raise PrintImageException(e.message) if img_request.status_code != requests.codes.ok: raise PrintImageException('Image fetching error, reason: ' 'Response status code is not 200 OK') else: with open(tmp_img_path, 'wb') as f: # img_request.raw.decode_content = True shutil.copyfileobj(img_request.raw, f) del img_request logger.debug('Image downloaded from url: {}'.format(image_url)) if platform.system() == 'Linux' or platform.system() == 'Darwin': logger.debug('Printing on UNIX system...') lpr_call = subprocess.Popen(['lpr', tmp_img_path], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) lpr_stdout = lpr_call.stdout.read() if 'Error' in lpr_stdout: raise PrintImageException(lpr_stdout) elif platform.system() == 'Windows': logger.debug('Printing on Windows system...') import win32print import win32ui printer_name = win32print.GetDefaultPrinter() # http://timgolden.me.uk/python/win32_how_do_i/print.html#rough_and_ready # # Create a device context from a named printer # and assess the printable size of the paper. hDC = win32ui.CreateDC() hDC.CreatePrinterDC(printer_name) bmp = Image.open(tmp_img_path) # Start the print job, and draw the bitmap to # the printer device hDC.StartDoc(tmp_img_path) hDC.StartPage() dib = ImageWin.Dib(bmp) dib.draw(hDC.GetHandleOutput(), (1, 1, 1219, 1829)) hDC.EndPage() hDC.EndDoc() hDC.DeleteDC() os.remove(tmp_img_path)
def send_to_printer(imagename): # # Constants for GetDeviceCaps # # # HORZRES / VERTRES = printable area # HORZRES = 8 VERTRES = 10 # # LOGPIXELS = dots per inch # LOGPIXELSX = 88 LOGPIXELSY = 90 # # PHYSICALWIDTH/HEIGHT = total area # PHYSICALWIDTH = 110 PHYSICALHEIGHT = 111 # # PHYSICALOFFSETX/Y = left / top margin # PHYSICALOFFSETX = 112 PHYSICALOFFSETY = 113 printer_name = win32print.GetDefaultPrinter() # file_name = "test.jpg" file_name = imagename # # You can only write a Device-independent bitmap # directly to a Windows device context; therefore # we need (for ease) to use the Python Imaging # Library to manipulate the image. # # Create a device context from a named printer # and assess the printable size of the paper. # hDC = win32ui.CreateDC() hDC.CreatePrinterDC(printer_name) printable_area = hDC.GetDeviceCaps(HORZRES), hDC.GetDeviceCaps(VERTRES) printer_size = hDC.GetDeviceCaps(PHYSICALWIDTH), hDC.GetDeviceCaps( PHYSICALHEIGHT) printer_margins = hDC.GetDeviceCaps(PHYSICALOFFSETX), hDC.GetDeviceCaps( PHYSICALOFFSETY) # # Open the image, rotate it if it's wider than # it is high, and work out how much to multiply # each pixel by to get it as big as possible on # the page without distorting. # bmp = Image.open(file_name) if bmp.size[0] > bmp.size[1]: bmp = bmp.rotate(90) ratios = [ 1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1] ] scale = min(ratios) # # Start the print job, and draw the bitmap to # the printer device at the scaled size. # hDC.StartDoc(file_name) hDC.StartPage() dib = ImageWin.Dib(bmp) scaled_width, scaled_height = [int(scale * i) for i in bmp.size] x1 = int((printer_size[0] - scaled_width) / 2) y1 = int((printer_size[1] - scaled_height) / 2) x2 = x1 + scaled_width y2 = y1 + scaled_height dib.draw(hDC.GetHandleOutput(), (x1, y1, x2, y2)) hDC.EndPage() hDC.EndDoc() hDC.DeleteDC()
def print_img(doc_name, file_names, printer_name): # # Constants for GetDeviceCaps # # # HORZRES / VERTRES = printable area # HORZRES = 8 VERTRES = 10 # # LOGPIXELS = dots per inch # LOGPIXELSX = 88 LOGPIXELSY = 90 # # PHYSICALWIDTH/HEIGHT = total area # PHYSICALWIDTH = 110 PHYSICALHEIGHT = 111 # # PHYSICALOFFSETX/Y = left / top margin # PHYSICALOFFSETX = 112 PHYSICALOFFSETY = 113 # printer_name = win32print.GetDefaultPrinter() # file_name = "1.jpg" # # You can only write a Device-independent bitmap # directly to a Windows device context; therefore # we need (for ease) to use the Python Imaging # Library to manipulate the image. # # Create a device context from a named printer # and assess the printable size of the paper. # hDC = win32ui.CreateDC() hDC.CreatePrinterDC(printer_name) printable_area = hDC.GetDeviceCaps(HORZRES), hDC.GetDeviceCaps(VERTRES) printer_size = hDC.GetDeviceCaps(PHYSICALWIDTH), hDC.GetDeviceCaps( PHYSICALHEIGHT) printer_margins = hDC.GetDeviceCaps(PHYSICALOFFSETX), hDC.GetDeviceCaps( PHYSICALOFFSETY) # # Start the print job, and draw the bitmap to # the printer device at the scaled size. # hDC.StartDoc(doc_name) hDC.StartPage() for file_name in file_names: # print file_name bmp = Image.open(file_name) # if bmp.size[0] > bmp.size[1]: # bmp = bmp.rotate(90) ratios = [ 1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1] ] scale = min(ratios) dib = ImageWin.Dib(bmp) scaled_width, scaled_height = [int(scale * i) for i in bmp.size] x1 = int((printer_size[0] - scaled_width) / 2) y1 = int((printer_size[1] - scaled_height) / 2) x2 = x1 + scaled_width y2 = y1 + scaled_height dib.draw(hDC.GetHandleOutput(), (x1, y1, x2, y2)) hDC.EndPage() hDC.EndDoc() hDC.DeleteDC()
def get_bach(): get_bach = str(leather_field.get()) get_loss = str(loss_calc_field.get()) get_type = str(leather_type.get()) barcode_path = os.path.join(tempfile.gettempdir(), 'barcode.png') file_path = os.path.join(tempfile.gettempdir(), 'file.png') if os.path.isfile(barcode_path): os.remove(barcode_path) if os.path.isfile(file_path): os.remove(file_path) if get_bach == '': messagebox.showinfo("Партія шкіри:", "Поле партії не може бути пустим!") elif not get_type in select_type: messagebox.showinfo("Тип шкіри:", "Оберіть тип шкіри!") elif get_loss == '' or len(get_loss) < 6: messagebox.showinfo( "№ Розрахунку витрат:", "Поле розрахунку витрат не може бути пустим, або містити не відповідні дані!" ) elif get_bach != '' and get_loss != '': if __name__ == '__main__': Ean() subprocess.call(['attrib', '+h', barcode_path]) g_etik = count_field.get() if int(g_etik) > 99: messagebox.showinfo( "Увага!", "Перевірте коректність вводу кількості етикеток!") count_field.delete(0, tk.END) count_field.insert(0, 1) txt = Image.open(barcode_path) W, H = txt.size fnt = ImageFont.truetype("arial.ttf", 36) fnt_t = ImageFont.truetype("arial.ttf", 18) d = ImageDraw.Draw(txt) w, h = d.textsize(get_loss) w1, h1 = d.textsize(get_type) d.text(((float(W / 2) - (w * 1.5)), (float(H / 3) + (h * 2))), get_loss, font=fnt, fill=(0, 0, 0, 255)) d.text(((float(W / 2) - (w1 * 0.7)), (float(H / 2.2) + (h1 * 5.1))), get_type, (0, 0, 0), font=fnt_t) # fill=(255, 255, 255, 255) txt.save(file_path, "PNG") subprocess.call(['attrib', '+h', file_path]) txt.close() file_name = file_path i = int(g_etik) if int(g_etik) <= 99: while i > 0: # Add loss calculation number to barcode # Print finish result HORZRES = 8 VERTRES = 10 # LOGPIXELS = dots per inch LOGPIXELSX = 88 LOGPIXELSY = 90 # PHYSICALWIDTH/HEIGHT = total area PHYSICALWIDTH = 110 PHYSICALHEIGHT = 111 # PHYSICALOFFSETX/Y = left / top margin PHYSICALOFFSETX = 112 PHYSICALOFFSETY = 113 #printer_name = win32print.GetDefaultPrinter() #file_name = "file.png" # You can only write a Device-independent bitmap # directly to a Windows device context; therefore # we need (for ease) to use the Python Imaging # Library to manipulate the image. # # Create a device context from a named printer # and assess the printable size of the paper. hDC = win32ui.CreateDC() hDC.CreatePrinterDC(select.get()) printable_area = hDC.GetDeviceCaps(HORZRES), hDC.GetDeviceCaps( VERTRES) printer_size = hDC.GetDeviceCaps( PHYSICALWIDTH), hDC.GetDeviceCaps(PHYSICALHEIGHT) printer_margins = hDC.GetDeviceCaps( PHYSICALOFFSETX), hDC.GetDeviceCaps(PHYSICALOFFSETY) # Open the image, rotate it if it's wider than # it is high, and work out how much to multiply # each pixel by to get it as big as possible on # the page without distorting. bmp = Image.open(file_name) if bmp.size[0] > bmp.size[1]: bmp = bmp.rotate(0) ratios = [ 1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1] ] scale = min(ratios) # Start the print job, and draw the bitmap to # the printer device at the scaled size. hDC.StartDoc(file_name) hDC.StartPage() dib = ImageWin.Dib(bmp) scaled_width, scaled_height = [ int(scale * i) for i in bmp.size ] x1 = int((printer_size[0] - scaled_width) / 2) y1 = int((printer_size[1] - scaled_height) / 2) x2 = x1 + scaled_width y2 = y1 + scaled_height dib.draw(hDC.GetHandleOutput(), (x1, y1, x2, y2)) hDC.EndPage() hDC.EndDoc() hDC.DeleteDC() i -= 1 bmp.close() loss_calc_field.delete(0, tk.END) leather_field.delete(0, tk.END) leather_type.delete(0, tk.END) os.remove(barcode_path) os.remove(file_path)
# PHYSICALWIDTH/HEIGHT = total area PHYSICALWIDTH = 110 PHYSICALHEIGHT = 111 # PHYSICALOFFSETX/Y = left / top margin PHYSICALOFFSETX = 112 PHYSICALOFFSETY = 113 # You can only write a Device-independent bitmap # directly to a Windows device context; therefore # we need (for ease) to use the Python Imaging # Library to manipulate the image. # Create a device context from a named printer # and assess the printable size of the paper. hDC = win32ui.CreateDC() hDC.CreatePrinterDC(printer) printable_area = hDC.GetDeviceCaps(HORZRES), hDC.GetDeviceCaps(VERTRES) printer_size = hDC.GetDeviceCaps(PHYSICALWIDTH), hDC.GetDeviceCaps(PHYSICALHEIGHT) printer_margins = hDC.GetDeviceCaps(PHYSICALOFFSETX), hDC.GetDeviceCaps(PHYSICALOFFSETY) # Open the image, rotate it if it's wider than # it is high, and work out how much to multiply # each pixel by to get it as big as possible on # the page without distorting. bmp = Image.open(pathToImageFile) if bmp.size[0] > bmp.size[1]: bmp = bmp.rotate(90) ratios = [1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1]] scale = min(ratios)