1

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 in python3 :

    #!/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:

    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

feri
  • 31
  • 1
  • 7
  • seemingly a duplicate of [print a persian file by thermal printer and python-escpos mnodule](https://python-forum.io/Thread-print-a-persian-file-by-thermal-printer-and-python-escpos-mnodule) on python-forum and [print a persian file by thermal printer and python-escpos mnodule](https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=190531) on Raspberry Pi Forums. – scruss Aug 14 '17 at 15:49

0 Answers0