从Python将阿拉伯语单词打印到ESC/POS打印机? [英] Print Arabic words from Python to ESC/POS printer?

查看:465
本文介绍了从Python将阿拉伯语单词打印到ESC/POS打印机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Odoo实现,我需要在ESC/POS打印机上打印阿拉伯语单词.

I have an Odoo implementation, and I need to print Arabic words to an ESC/POS printer.

Odoo社区已经开发了一个Python模块,该模块将UTF-8文本转换为ESC/POS代码页.问题是,当我打印阿拉伯文本时,我得到的是反向文本和断开的字母.

The Odoo community has already developed a Python module that translates UTF-8 text to ESC/POS Code Page. The problem is that when I print Arabic text I'm getting reversed text and disconnected letters.

如何从Python打印正确的阿拉伯语单词到ESC/POS?

How do I print correct Arabic word from Python to ESC/POS?

请参见 Escpos.text escpos.py中的方法,以供参考.

See the Escpos.text method from escpos.py for reference.

推荐答案

如注释中所述,这不是在嵌入式设备上正确显示UTF-8阿拉伯文本的简单任务.您需要处理文本方向,连接和字符编码.

As noted in the comments, it is not a trivial task display UTF-8 Arabic text correctly on an embedded device. You need to handle text direction, joining and character encoding.

我过去曾尝试使用 PHP ESC/POS驱动程序由我维护,并且无法在本机ESC/POS中加入阿拉伯字符.但是,我最终还是选择了

I've had an attempt at this in the past for a PHP ESC/POS driver that I maintain, and was unable to get joined Arabic characters in native ESC/POS. However, I did end up settling on this workaround (PHP) that printed images instead.

解决此问题的基本步骤是:

The basic steps to working around this are:

  • 获取阿拉伯字体,一些文本库和图像库
  • 加入(重塑")角色
  • 使用比迪文本布局算法将UTF-8转换为LTR(打印)顺序
  • 将其拍在图像上,并右对齐
  • 打印图像

要将其移植到python,我在此答案上使用了魔杖. Python图片库(PIL)将变音符号显示为单独的字符,从而使输出不适合.

To port this to python, I lent on this answer using Wand. The Python Image Library (PIL) was displaying diacritics as separate characters, making the output unsuitable.

相关性在注释中列出.

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

# Some variables
fontPath = "/usr/share/fonts/opentype/fonts-hosny-thabit/Thabit.ttf"
textUtf8 = u"بعض النصوص من جوجل ترجمة"
tmpImage = 'my-text.png'
printFile = "/dev/usb/lp0"
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 = fontPath
draw.font_size = 36
draw.text(printWidth, 22, textDisplay)
draw(im)
im.save(filename=tmpImage)

# Print an image with your printer library
printer = printer.File(printFile)
printer.set(align="right")
printer.image(tmpImage)
printer.cut()

运行该脚本会为您提供PNG,并将其打印到位于"/dev/usb/lp0"的打印机上.

Running the script gives you a PNG, and prints the same to a printer at "/dev/usb/lp0".

这是一个独立的 python-escpos 演示,但我假设Odoo具有类似的对齐和图像输出命令.

This is a standalone python-escpos demo, but I'm assuming that Odoo has similar commands for alignment and image output.

免责声明:我什至不会说阿拉伯语或说阿拉伯语,所以我不确定这是正确的.我只是在视觉上将打印输出与Google翻译给我的内容进行比较.

Disclaimer: I don't speak or write Arabic even slightly, so I can't be sure this is correct. I'm just visually comparing the print-out to what Google translate gave me.

这篇关于从Python将阿拉伯语单词打印到ESC/POS打印机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆