I'm trying to print a receipt using a thermal printer, this is the best solution I got so far using win32ui but I need to insert a logo or QR code
import win32ui
doc = open("1.txt", 'r').readlines()
f = 50
dc = win32ui.CreateDC()
dc.CreatePrinterDC()
dc.StartDoc('Test')
dc.StartPage()
for i, text in enumerate(doc):
print(i, text)
dc.TextOut(0,i*f, text)
dc.MoveTo(0, i*f)
dc.EndPage()
PyWin32 homepage: [GitHub]: mhammond/pywin32 - pywin32.
After "a bit" of researching:
[ME.TimGolden]: Print (and related pages)
[ReadTheDocs.Pillow]: ImageWin Module (Windows-only)
[MS.Docs]: Windows GDI
Other resources
I was able to come up with a dummy example (it also uses [PyPI]: Pillow - I wanted to use it just for reading the images (also tried OpenCV), but in the end it does almost all the work).
code00.py:
Notes:
To avoid printing each time I ran the program (and there were lots of them), I "printed" to a .pdf
I centered the image in the .pdf page (in a dummy manner) only considering printable area (ignoring printer physical properties, V / H scaling, ...). A robust (production ready) implementation should take all into account, fact that will make it waay more complex
Output:
The generated test.pdf:
And a page from my (Epson) printer (with
out_file = None). Colors are a bit off as black is very low and it attempted to synthetize it from CMY (and also it's late night and the phone that I took the picture with is old):You could check [SO]: Square in Python GDI (with PatBLT) (@CristiFati's answer) for another GDI example from Python (and PyWin32).