Unable to print in Arabic on Xprinter XP-P810

when I try to send text content to my thermal printer it gives me the chinese symbols in case of Arabic and in case in English it give me the correct phrase

could you help me with this problem this is image that describe what I mean :

:point_down:

I thought firstly that my printer didn't support Arabic but when I Used this app to send Arabic content it printed successfully

this is my function block :

 @SimpleFunction(description = "encode text")
public static List<Byte> generatePrintCommand(String charset, String text, boolean changeFont, int size, boolean bold, boolean underline, boolean feedline) {
    List<Byte> command = new ArrayList<>();

    // Set character encoding
    command.add((byte) 27);
    command.add((byte) 116);
    command.add((byte) charset.getBytes()[0]);

    // Add text to print
    command.add((byte) 27);
    command.add((byte) 33);
    int font = (changeFont ? 1 : 0) + (size << 2) + (bold ? 8 : 0) + (underline ? 128 : 0);
    command.add((byte) font);

    byte[] textBytes = text.getBytes();
    for (byte b : textBytes) {
        command.add(b);
    }

    // Feed line after printing
    if (feedline) {
        command.add((byte) 10);
        command.add((byte) 13);
    }

    return command;
}

this what I tried after connecting with printer :

Hello Black Night - what font are you trying to use? A bespoke Arabic font or a Unicode font? Could it be that you need to load the font (True Type) into your App?

really I don't know :sweat_smile: cause this is my first time dealing with thermal printer but I use this printer "Xprinter XP-P810"
What I want is to print Arabic successfully as English text

@ChrisWard but after adding the font to the app how can I use it ?

Actually, looking at the printer specs:

.... looks like you can't. The method with an App Font produces an image of the document to be printed but it would seem images are not supported by the XP-P810.

However, Epson ESC/POS is supported. That may mean the XP-P810 supports Unicode - first thing to to do is ask the printer manufacturer if that is so, via the link above (message or chat).

Epson ESC/POS code:

Edit: Ask xprinter which ESC/POS commands are supported - even the Epson printers do not support all of them.

1 Like

ABG - any ideas to output Arabic with this little printer? I was thinking of a Blocks List of the Arabic Unicode Character codes, build the document via a translation to code from Arabic Text in a Textbox set by the App User.

Similar to:

%D9%86 = ن

%D8%AC = ج

%D9%8A = ي

%D8%A8 = ب

%D9%85 = م

%D8%AD = ح

%D9%81 = ف

%D9%88 = و

%D8%B8 = ظ

1 Like

This is a mess.

An Internet search for 'printing Arabic gets Chinese ' returns lots of hits outside of AI2.

Most of them are unsuccessful, or have to resort to hacks like generating a bmp image and feeding its bytes to the printer after the code to switch to bmp mode.

Can AI2 print Arabic on a canvas?

Can that Canvas be exported as an image and converted to bmp format then be read back as bytes?

Probably need a JavaScript stunt for that.

Throw in the right to left print direction of Arabic cursive text.

Print extensions just gloss over code conversions.

Lots of luck asking the extension author for support.

I think we can do it without an extension. If the ESC/POS used by the printer accepts images, the document could be laid out on a Canvas, but probably easier to just use a TextBox if the document is something like a receipt. Even though the Arabic text is read right-to-left by the User, it doesn't matter that App Inventor will read it left-to-right (for conversion), it will still be correct on the document.

1 Like

So did you get information from the manufacturer about the ESC/POS commands supported?

@ChrisWard The Xprinter XP-P810 supports generic ESC/POS commands, which are a set of commands for controlling the formatting and printing of thermal printers

I also made some web searching:

To print any language like Arabic, you need to do two things:

Here is an example of how to print the Arabic text “شركة الحياة” on the Xprinter XP-P810 using Android Studio:

try {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH}, InvoiceActivity_AR.PERMISSION_BLUETOOTH);
    } else {
        BluetoothConnection connection = BluetoothPrintersConnections.selectFirstPaired();
        if (connection != null) {
            EscPosPrinter printer = new EscPosPrinter(connection, 203, 78f, 48);
            // Set the code page to 22
            final String codePage = new String(new byte[]{0x1B, 0x74, 0x16});
            // Encode the text in Windows-1256
            final String text = new String("شركة الحياة".getBytes("Windows-1256"));
            // Print the text
            printer.printFormattedText(codePage + text);
        } else {
            Toast.makeText(this, "No printer was connected!", Toast.LENGTH_SHORT).show();
        }
    }
} catch (Exception e) {
    Log.e("APP", "Can't print", e);
}

But I still can't convert it to a Java function to send the command to the printer,

if you can help I would be thankful

Hi Black_Knight

  1. You can't assume the Xprinter supports all of the ESC/POS commands, you need to ask the manufacturer what it supports, which is likely to be a subset.

  2. If the Xprinter supports Unicode via ESC/POS, you don't need the language code page nor Windows-125614 encoding, hence why I said chat with the Xprinter people.

  3. Even if you do need to set the code page and Windows-125614 encoding, you do not need a Java function to send the commands to the printer, you can send using App Inventor's Bluetooth Classic or App Inventor's BLE extension - you need to know the version of Bluetooth that the printer uses.

1 Like

AppInventor uses UTF8 encoding by default. You must decode the given text from AppInventor into bytes and then encode it to Windows-1256.

1 Like
byte[] utf8Bytes = textFromAi2.getBytes(Charset.forName("UTF-8"));
String textToPrint = new String(utf8Bytes, Charset.forName("Windows-1256"));

is this will return data as a list of bytes ?
so I can send it to the printer to print the text.

If you don't want Text, just bytes, use only the first line of code.

final String text = new String("شركة الحياة".getBytes("Windows-1256"));
            // Print the text
            printer.printFormattedText(codePage + text);

But I see that the printer accepts text, not bytes.

1 Like

OK, I will try both of them I hope this will solve my problem of printing Arabic text data .

and thanks @Patryk_F I really appreciate your help

1 Like

I look at your code and you actually use utf 8 encoding because getBytes(); adopts the default encoding of the platform, in our case Android, where the standard is Utf8.

I'm wondering about this code:
command.add((byte) charset.getBytes()[0]);

What is this "UTF-8".getByte()[0]; method supposed to return?

Why don't you try setting this byte to 22?

And have you tried it?

byte[] textBytes = text.getBytes("Windows-1256");