0

The code generates a QR code and prints it, but It is not working on the Debian Os due to not supporting the imported libraries (win32print, Win32ui). Can anyone tell me how to run it on the Debian without changing the whole code.


from random import randint
import win32print
import win32ui

from PIL import Image, ImageWin
from PIL._imaging import font
from PIL import ImageFont
from PIL import ImageDraw



    HORZRES = 8
    VERTRES = 10

   LOGPIXELSX = 88
   LOGPIXELSY = 90

   PHYSICALWIDTH = 110
   PHYSICALHEIGHT = 111


   PHYSICALOFFSETX = 112
   PHYSICALOFFSETY = 113


__author__ = 'masoodhussain'

import qrcode
import subprocess
import os

qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=10,
    border=4,
)

qr.add_data('Masooddkjfdlfs,kokdfds sddshfhkjshfljsdhkjfdrtyyhtfhfghgh3')
qr.make(fit=True)

"subprocess.call(['lp', 'foo.png'])"

printer_name = win32print.GetDefaultPrinter()

img = qr.make_image()

img.show()

random_number= randint(0,10000)

img.save('label_'+str(random_number)+'.png')

file_name = 'label_'+str(random_number)+'.png'

print(file_name)


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)


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)


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 ()

when I run the code by removing the unsupported libraries it gives an error on this part: error importing

import qrcode

I am trying to import whole folder for using there other files. In Windows it was working perfectly. Any help would be appreciated.Thanks

مسعود
  • 679
  • 10
  • 25
  • First you need to make sure you have qrcode installed. You can check this by running the following command from the temrinal: pip freeze | grep qrcode – Tom May 01 '16 at 16:19

2 Answers2

1

This code is equivalent to the code posted in Question.

from random import randint
import cups

from PIL import Image, ImageWin
from PIL._imaging import font
from PIL import ImageFont
from PIL import ImageDraw


__author__ = 'masoodhussain'

import qrcode

qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=5,
    border=2,
)

qr.add_data('localhost:5070productinfo')
qr.make(fit=True)

conn= cups.Connection()
printer_name = conn.getPrinters()

printer_name = printer_name.keys()[0]

printqueuelength = len(conn.getJobs())

img = qr.make_image()

img.show()

random_number= randint(0,10000)

img.save('label_'+str(random_number)+'.png')

file_name = 'label_'+str(random_number)+'.png'

print(file_name)

conn.printFile(printer_name,file_name,"Hello", options ={'media':'25x25mm'}) 

Important part is the installation of required libraries and changing your media to the required size.

مسعود
  • 679
  • 10
  • 25
0

Even if you install qrcode, your code will still fail because of the Windows specific library. You need to check on which system you are working and preferably put the whole print function in a separate function.
Here are some useful links: https://stackoverflow.com/a/1857/2776376 and https://pypi.python.org/pypi/pycups

import platform

if platform.system() = 'Linux':
    import libcups
elif platform.system() = 'Windows':
    import win32print
    import win32ui
else:
    print('Unsupported OS. Exiting....')
    sys.exit(1)

def my_printer_function():
    if platform.system() = 'Linux':
        #now call the Linux printer
    elif platform.system() = 'Windows':
        #use your old Windows code
Community
  • 1
  • 1
Maximilian Peters
  • 30,348
  • 12
  • 86
  • 99
  • I have a problem. I have converted the code using pycups and it is printing the qr code but it is printing only a portion of the image. How can I adjust the image in pycups, I have tried conn.printFile(printer_name,file_name,"Hello ", {'fit-to-page':'True'}) but the result is same – مسعود May 23 '16 at 14:45
  • The last portion of the above code was adjusting the image but now I have removed all this portion so its not adjusting the image – مسعود May 23 '16 at 14:48
  • is the printed image bigger than the page or does it place the image on the wrong part of the page? – Maximilian Peters May 23 '16 at 16:23
  • the printed image is bigger than the page. only 1/4 Qr code is printed on the full Qr sticker – مسعود May 24 '16 at 00:39
  • Does this also happen when you call `cups` from the command line? You could also try using `scaling=100`. – Maximilian Peters May 24 '16 at 07:41
  • I am running the complete code from terminal.The system is a debian system. It seems to have no effect if I use scaling or fit to page – مسعود May 24 '16 at 13:17
  • I am not sure whats the problem. code for windows which is posted above works fine but the new one I made for linux only print a part of the sticker – مسعود May 24 '16 at 13:19
  • The printer is Zebra Gk420t with small stickers attached on the printer roll – مسعود May 24 '16 at 13:20
  • Thanks for your help I found something in options which print correct image. – مسعود May 24 '16 at 14:02