python-escpos / python-escpos

Python library to manipulate ESC/POS printers
https://python-escpos.readthedocs.io
MIT License
1.05k stars 294 forks source link

Arabic Text not printing correctly #633

Open BaselAbdallah opened 7 months ago

BaselAbdallah commented 7 months ago

I have:

Bug description

•• Arabic Text not printing correctly:

• when testing this code: from escpos import printer x = printer.Network("192.168.192.168") text = "عصير" x.text(text) x.cut() It printed the text reversed "from left to right like that" and each character separated from the other. It printed like this: "ر ي ص ع"

• Then I tested using the code page "CP864" like this: from escpos import printer x = printer.Network("192.168.192.168") x.charcode(code="CP864") text = "عصير" x.text(text) x.cut() It printed question marks instead of the Arabic characters. I tried using "CP720" and the result was similar to the first case, printed reversed and separated.

• Then I tried using _raw() to print instead of text() like this: from escpos import printer x = printer.Network("192.168.192.168") x.charcode(code="CP864") text = عصير" def print_arabic_text(text): reversed_text = text[::-1] return reversed_text x._raw(f"{print_arabic_text(text)}".encode("Arabic")) x.cut() exit() The result was close to correct Arabic text, but not good enough.

•• How to print correct Arabic text?

Steps to reproduce

Device info

Printer: Epson TM-T20III

python version: 3.12.2

operating system: Windows 11

belono commented 6 months ago

Hi @BaselAbdallah and welcome to python-escpos!

You can add the profile=’TM-T20II’ parameter to the printer's instance. It will instruct python-escpos which charcode sets are supported by your printer and encode your text string properly. You can then force your printer to select an specific charcode set, but keep in mind that your printer must support that charcode set.

from escpos import printer
x = printer.Network("192.168.192.168", profile=’TM-T20II’)
# x.charcode(code="CP864")
x.charcode(code="CP720")  # Correct
text = "عصير"
x.textln(text)
x.cut()

Does this work for your printer?

EDIT on 11-May: The correct charcode seems to be "CP720"

belono commented 6 months ago

Hi @BaselAbdallah !

I had some time to take a second look at your issue.

It looks like "CP720" is the correct charcode and it is also supported by your printer. You only have to pass the profile="TM-T20II" parameter when instancing the connector and let the magicencode feature auto-detect the string encoding. It works with the Dummy connector so it should do with the other connectors.

In response to the alignment of the text, python-escpos defaults to left aligned text. Did you try to set the printer to right align the characters?

from escpos import printer

x = printer.Network("192.168.192.168", profile=’TM-T20II’)
x.charcode(code="CP720")  # Surely you can omit this command
x.set(align="right")
text = "عصير"
x.textln(text)
x.cut()
x.close()

Please, let us know if this works as many users would find this information useful.