gray_farz
Posts: 32
Joined: Mon Mar 20, 2017 6:13 am

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

Thu Aug 10, 2017 5:14 am

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

Code: Select all

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

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

User avatar
topguy
Posts: 3316
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

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

Sun Aug 13, 2017 1:30 pm

It doesnt matter what encoding you use unless you have a font in the printer with the correct symbols. Have you verified this ?
The printer have a pretty complex menu system you can access with holding in line-feed while turning it on. ( if I recall correctly )

User avatar
scruss
Posts: 1279
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

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

Sun Aug 13, 2017 2:18 pm

Does the my-text.png file generated by your first script look correct to you? If so, the problem is more that your printer isn't printing bitmaps correctly.

Your second codepage example might need some conversion from utf-8 to 8859-6. It will also need the right font in the printer.

I find that Python 3 is much more consistent in handling Unicode than Python 2. Check that there are Py3-compatible versions of your modules, and change the first line to

Code: Select all

#!/usr/bin/env python3
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

gray_farz
Posts: 32
Joined: Mon Mar 20, 2017 6:13 am

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

Mon Aug 14, 2017 4:22 am

Does the my-text.png file generated by your first script look correct to you? If so, the problem is more that your printer isn't printing bitmaps correctly.
i have printed jpg and gif images by my printer.so i don't think my printer has prooblem in printing bitmaps
Your second codepage example might need some conversion from utf-8 to 8859-6. It will also need the right font in the printer.

i have choosen 8859-6 as a test(accidently)..i don't really know what codepage i must choose?? and how i can understand it
I find that Python 3 is much more consistent in handling Unicode than Python 2. Check that there are Py3-compatible versions of your modules, and change the first line to
python-escpos was installed on python3,too..i now work by python3...what module is compatible by python3????

gray_farz
Posts: 32
Joined: Mon Mar 20, 2017 6:13 am

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

Mon Aug 14, 2017 5:32 am

It doesnt matter what encoding you use unless you have a font in the printer with the correct symbols. Have you verified this ?
my printer is TM-T20...I don't my printer's fonts...i couldn't it from its manual..
The printer have a pretty complex menu system you can access with holding in line-feed while turning it on. ( if I recall correctly )
i turned the printer on and then holded the line feed button but it prints blank..i don't know how i can access this menu

User avatar
topguy
Posts: 3316
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

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

Mon Aug 14, 2017 9:13 am

You have to hold the button in while turning it on.

User avatar
scruss
Posts: 1279
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: print a persian file by thermal printer and python-escpos module

Mon Aug 14, 2017 3:56 pm

gray_farz wrote:
Mon Aug 14, 2017 4:22 am
i have printed jpg and gif images by my printer.so i don't think my printer has problem in printing bitmaps
Okay, that's a start, but your first example code at line 46 says:

Code: Select all

im.save(filename=tmpImage)
and tmpImage is defined as my-text.png (line 27). So what I'm asking is:
  1. Does the file my-text.png get created?
  2. Does the image look at all like the text "بعض النصوص من جوجل ترجمة"?
I see you've asked a couple of other places like Python Forum and Stack Overflow.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

gray_farz
Posts: 32
Joined: Mon Mar 20, 2017 6:13 am

Re: print a persian file by thermal printer and python-escpos module

Tue Aug 15, 2017 4:17 am

[*]Does the file my-text.png get created?
[*]Does the image look at all like the text "بعض النصوص من جوجل ترجمة"?
i think it is created...because it prints the question mark '?' exactly the same number of letters of "بعض النصوص من جوجل ترجمة"
the space of between the letters is preserved,too...
as the following :
????? ???? ?? ??????‌ ???

gray_farz
Posts: 32
Joined: Mon Mar 20, 2017 6:13 am

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

Tue Aug 15, 2017 4:29 am

topguy wrote:
Mon Aug 14, 2017 9:13 am
You have to hold the button in while turning it on.
holding 'feed' button gave me the following contents:
Image
Attachments
feedd.jpg
contents of feed button
feedd.jpg (116.78 KiB) Viewed 120 times

Return to “Python”

Who is online

Users browsing this forum: No registered users and 12 guests