2

I have this code on windows which is working with A4 printers. But i have a very small printer, when i print something it does not print anything but only push the ticket out.

enter image description here

TRY0:

import win32ui
X=0; Y=0
input_string = "Print 1234 test"
multi_line_string = input_string.split()
hDC = win32ui.CreateDC ()
hDC.CreatePrinterDC ('Dymo label printer')
hDC.StartDoc ('FILE NAME FILE NAME')
hDC.StartPage ()
for line in multi_line_string:
     hDC.TextOut(X,Y,line)
     #Y += 100
hDC.EndPage ()
hDC.EndDoc ()

TRY1: faling to print same like before: https://msdn.microsoft.com/en-us/library/windows/desktop/dd319099(v=vs.85).aspx

import win32ui
import win32con
import win32print
import win32gui

X=0
Y=0
input_string = "A390"

printer = win32print.GetDefaultPrinter()
hprinter = win32print.OpenPrinter(printer)
devmode = win32print.GetPrinter(hprinter, 2)["pDevMode"]

# change paper size and orientation
# constants are available here:
# http://msdn.microsoft.com/library/default.asp?
#      url=/library/en-us/intl/nls_Paper_Sizes.asp
# number 10 envelope is 20
devmode.PaperSize = 20
# 1 = portrait, 2 = landscape
devmode.Orientation = 1

hdc = win32gui.CreateDC("WINSPOOL", printer, devmode)
dc = win32ui.CreateDCFromHandle(hdc)
dc.StartDoc ('test')
dc.StartPage ()
dc.TextOut(X,Y,input_string)
#for line in multi_line_string:
     #hDC.TextOut(X,Y,line)
dc.EndPage ()
dc.EndDoc ()

TRY2: FAIL

import win32ui
import win32con
import win32print
import win32gui

printer = win32print.GetDefaultPrinter()
hprinter = win32print.OpenPrinter(printer)
devmode = win32print.GetPrinter(hprinter, 2)["pDevMode"]
devmode.PaperSize = 20
devmode.Orientation = 2

hDC = win32gui.CreateDC ("WINSPOOL", printer, devmode)
dc = win32ui.CreateDCFromHandle(hDC)
dc.StartDoc ('test')
dc.StartPage ()

#hDC.SetMapMode(MM_TEXT) # ERROR no MM_TEXT found...
font = win32ui.CreateFont({
    "name": "Lucida Console",
    "height": 50,
    "weight": 50,
})
win32print.DocumentProperties(0, hprinter, '300LN1', None, None, 5)
dc.SelectObject(font)
dc.TextOut(10,10,"TEST...")
dc.TextOut(10,-10,"TEST..")

dc.EndPage ()
dc.EndDoc ()

TRY3: FAIL

import win32ui
import win32print
import win32con

INCH = 1440

hDC = win32ui.CreateDC ()
hDC.CreatePrinterDC (win32print.GetDefaultPrinter ())
hDC.StartDoc ("Test doc")
hDC.StartPage ()
hDC.SetMapMode (win32con.MM_TWIPS)
hDC.DrawText ("TEST", (0, INCH * -1, INCH * 8, INCH * -2), win32con.DT_CENTER)
hDC.EndPage ()
hDC.EndDoc ()
  • Hey, have you tried to print: "0,1,2,3,4.5.......,99999" to determine position in the page? left corner/center/rightcorner? otherwise I cannot find anything for small printer. – Destrif Jul 04 '16 at 07:03
  • How do i set left corner x=0 is left and y=0 means on top in A4 page. but when i do this with same small printer its printing nothing. –  Jul 04 '16 at 07:06
  • WE NEED TO SETUP THE PAGE SIZE TO 50.8X25.4MM. THEN X=0 AND Y=0 SHOULD WORK. But how can we tell Python on win32ui that page size is not A4 but its custom page size to 50.8x25.4mm ? –  Jul 04 '16 at 07:08
  • Yes I know... but I am not able to find it in documentation, please don't use caps lock. – Destrif Jul 04 '16 at 07:12
  • http://newcenturycomputers.net/projects/pythonicwindowsprinting.html - @Destrif, can you open that url and make a sample of his last part please? let me check if that works i am getting lost on his url last part, what he was trying to say, there he mentioned you can setup the paper size. –  Jul 04 '16 at 07:26
  • Please check my TRY1, still its failing to print. –  Jul 04 '16 at 07:34

1 Answers1

0

You should try this:

#MM_TEXT= Each logical unit is mapped to one device pixel. Positive x is to the right; positive y is down. OR to draw MM_TWIPS see link
hDC.SetMapMode(MM_TEXT)
font = win32ui.CreateFont({
    "name": "Lucida Console",
    "height": 10,
    "weight": 20,
})
win32print.DocumentProperties(0, pHandle, '300LN1', None, None, 5)
hDC.SelectObject(font)
#First print this test to know if it is inverted as user say it
# Also don't forget to keep **LONG TEXT TEST** to have chance to see it if not well placed.
hDC.TextOut(10,10,"This is a Test/ This is a second test...................")
hDC.TextOut(10,-10,"This is the bottom Test/ This is a second bottom test.................")

From:

http://newcenturycomputers.net/projects/pythonicwindowsprinting.html

https://msdn.microsoft.com/fr-fr/library/windows/desktop/dd162980(v=vs.85).aspx

Destrif
  • 2,104
  • 1
  • 14
  • 22
  • No still same. Its pushing out the paper but printing nothing at all. –  Jul 04 '16 at 07:45
  • See also : win32print.DocumentProperties(0, pHandle, '300LN1', None, None, 5), http://stackoverflow.com/questions/5555453/python-win32print-changing-advanced-printer-options , also: http://timgolden.me.uk/python/win32_how_do_i/print.html – Destrif Jul 04 '16 at 07:53
  • Please check my TRY2. Still same its not printing anything at all. –  Jul 04 '16 at 07:59
  • http://timgolden.me.uk/python/win32_how_do_i/print.html - all of them also not printing anything at all. –  Jul 04 '16 at 08:02
  • I found an example in c/c++ but it will help you I think http://www.cplusplus.com/forum/windows/31112/ – Destrif Jul 04 '16 at 08:33