#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2017
    Posts
    31
    Rep Power
    1

    Question print a persian file by thermal printer and python-escpos mnodule


    hello friends...
    i want to print a persian file by thermal printer..
    i can print an english file esily..but i face the proble while printing persian text..
    i think i don't do some principles...i have studied about unicode encodeand decode in python..but it seems it isn't enough
    please guide me

    my code is :
    Code:
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    # Print an Arabic string to a printer.
    # Based on example from escpos-php
    
    # Dependencies-
    # - pip install wand python-bidi python-escpos
    # - sudo apt-get install fonts-hosny-thabit
    # - download arabic_reshaper and place in arabic_reshaper/ subfolder
    
    import arabic_reshaper
    #from escpos import printer
    from escpos.printer import Usb
    from bidi.algorithm import get_display
    from wand.image import Image as wImage
    from wand.drawing import Drawing as wDrawing
    from wand.color import Color as wColor
    
    p = Usb(0x04b8,0x0e15,0)
    
    p.codepage="cp720"   #设置解码的类型
    
    # Some variables
    #fontPath = "/usr/share/fonts/opentype/fonts-hosny-thabit/Thabit.ttf"
    textUtf8 = u"بعض النصوص من جوجل ترجمة"
    tmpImage = 'my-text.png'
    printWidth = 550
    
    # Get the characters in order
    textReshaped = arabic_reshaper.reshape(textUtf8)
    textDisplay = get_display(textReshaped)
    
    # PIL can't do this correctly, need to use 'wand'.
    # Based on
    # https://stackoverflow.com/questions/5732408/printing-bidi-text-to-an-image
    im = wImage(width=printWidth, height=36, background=wColor('#ffffff'))
    draw = wDrawing()
    draw.text_alignment = 'right';
    draw.text_antialias = False
    draw.text_encoding = 'utf-8'
    draw.text_kerning = 0.0
    draw.font_size = 36
    draw.text(printWidth, 22, textDisplay)
    draw(im)
    im.save(filename=tmpImage)
    
    # Print an image with your printer library
    p.set(align="right")
    p.image(tmpImage)
    p.cut()
    in the above code, i used the different codepages, but my output from the printer is just question mark ""?"" instead of the persian words
    i tried the following code,too:

    Code:
    #coding=utf8
    
    from escpos.printer import Usb
    """ Seiko Epson Corp. Receipt Printer M129 Definitions (EPSON TM-T88IV) """
    
    p = Usb(0x04b8,0x0e15,0)
    
    p.codepage="iso8859_6"  
    
    # Print text
    p.text(u"سلام\n")
    
    p.cut()
    but the printer prints the obscure letters...
    i tried the different codepages..but it wasn't usefull
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,888
    Rep Power
    509
    The first approach looks best in which you have python create a graphic which then prints. Maybe convert to UTF8 first?
    Code:
    >>> a
    'بعض النصوص من جوجل ترجمة'
    >>> a.encode()  # convert to UTF8
    b'\xd8\xa8\xd8\xb9\xd8\xb6 \xd8\xa7\xd9\x84\xd9\x86\xd8\xb5\xd9\x88\xd8\xb5 \xd9\x85\xd9\x86 \xd8\xac\xd9\x88\xd8\xac\xd9\x84 \xd8\xaa\xd8\xb1\xd8\xac\xd9\x85\xd8\xa9'
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2017
    Posts
    31
    Rep Power
    1
    Originally Posted by b49P23TIvg
    The first approach looks best in which you have python create a graphic which then prints. Maybe convert to UTF8 first?
    Code:
    >>> a
    'بعض النصوص من جوجل ترجمة'
    >>> a.encode()  # convert to UTF8
    b'\xd8\xa8\xd8\xb9\xd8\xb6 \xd8\xa7\xd9\x84\xd9\x86\xd8\xb5\xd9\x88\xd8\xb5 \xd9\x85\xd9\x86 \xd8\xac\xd9\x88\xd8\xac\xd9\x84 \xd8\xaa\xd8\xb1\xd8\xac\xd9\x85\xd8\xa9'
    i did it...but i got the error..
    reshape function in arabic_reshaper module gets just string or buffer not bytes..
    moreover textDisplay argument in draw.text function must be string or buffer not bytes
    ...i use python3

IMN logo majestic logo threadwatch logo seochat tools logo