mazenrashed / Printooth

A well documented, high-level Android interface that makes printing via bluetooth printers easier
Mozilla Public License 2.0
400 stars 114 forks source link

Arabic String not work with thermal printer #62

Closed iman2420 closed 3 years ago

iman2420 commented 4 years ago

I use this function to add Persian text :


.setText("سلام : HELLO")
.setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD) //Bold or normal
.setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
.setUnderlined(DefaultPrinter.UNDERLINED_MODE_OFF) // Underline on/off
.setCharacterCode(byte) // Character code to support languages
.setLineSpacing(DefaultPrinter.LINE_SPACING_30)
.setNewLinesAfter(1)
printableList.add(printable.build())```

But only some incomprehensible text is printed.
ZakariaJawas commented 3 years ago

@iman2420 Finally I was able to fix this by passing a custom converter, it was already built with the library ArabicConverter() like this .setCustomConverter(ArabicConverter()) It is worth mentioning that CP864 codepage value might be different from printer company to another, CHARCODE_ARABIC_CP864 in the library equals to 0x0E, but with my printer company it was 22 inside their codepage table so you might need to check your printer documentation, then pass the custom character code like this .setCharacterCode(22)

amalnafia commented 3 years ago

@iman2420 Finally I was able to fix this by passing a custom converter, it was already built with the library ArabicConverter() like this .setCustomConverter(ArabicConverter()) It is worth mentioning that CP864 codepage value might be different from printer company to another, CHARCODE_ARABIC_CP864 in the library equals to 0x0E, but with my printer company it was 22 inside their codepage table so you might need to check your printer documentation, then pass the custom character code like this .setCharacterCode(22)

where i can codePage? I have tested the code in this project and it didn't work

ZakariaJawas commented 3 years ago

@iman2420 Finally I was able to fix this by passing a custom converter, it was already built with the library ArabicConverter() like this .setCustomConverter(ArabicConverter()) It is worth mentioning that CP864 codepage value might be different from printer company to another, CHARCODE_ARABIC_CP864 in the library equals to 0x0E, but with my printer company it was 22 inside their codepage table so you might need to check your printer documentation, then pass the custom character code like this .setCharacterCode(22)

where i can codePage? I have tested the code in this project and it didn't work

CodePage is a hex number, you should find something called CodePage Table in your printer company website or documentation so just pass these two lines with your TextPrintable builder

.setCharacterCode(22)
.setCustomConverter(ArabicConverter())
amalnafia commented 3 years ago

@iman2420 Finally I was able to fix this by passing a custom converter, it was already built with the library ArabicConverter() like this .setCustomConverter(ArabicConverter()) It is worth mentioning that CP864 codepage value might be different from printer company to another, CHARCODE_ARABIC_CP864 in the library equals to 0x0E, but with my printer company it was 22 inside their codepage table so you might need to check your printer documentation, then pass the custom character code like this .setCharacterCode(22)

where i can codePage? I have tested the code in this project and it didn't work

CodePage is a hex number, you should find something called CodePage Table in your printer company website or documentation so just pass these two lines with your TextPrintable builder

.setCharacterCode(22)
.setCustomConverter(ArabicConverter())

it did not work it printed Chinese letters

ZakariaJawas commented 3 years ago

@iman2420 Finally I was able to fix this by passing a custom converter, it was already built with the library ArabicConverter() like this .setCustomConverter(ArabicConverter()) It is worth mentioning that CP864 codepage value might be different from printer company to another, CHARCODE_ARABIC_CP864 in the library equals to 0x0E, but with my printer company it was 22 inside their codepage table so you might need to check your printer documentation, then pass the custom character code like this .setCharacterCode(22)

where i can codePage? I have tested the code in this project and it didn't work

CodePage is a hex number, you should find something called CodePage Table in your printer company website or documentation so just pass these two lines with your TextPrintable builder

.setCharacterCode(22)
.setCustomConverter(ArabicConverter())

it did not work it printed Chinese letters

Are you trying to print Arabic string? .setCharacterCode(22) will only works with my printer company, you should find the correct codepage for your printer Did you try the default CP864 codepage ?

ahmedsaber00 commented 2 years ago

جرب كده

public static byte[] POS_Print_Text(String pszString, String encoding, int codepage, int nWidthTimes, int nHeightTimes, int nFontType) {
        if (codepage < 0 || codepage > 255 || pszString == null || pszString.length() < 1) {
            return null;
        }
        try {
            byte[] pbString = pszString.getBytes(encoding);
            Command.GS_ExclamationMark[2] = (byte) (new byte[]{0, 16, 32, 48}[nWidthTimes] + new byte[]{0, 1, 2, 3}[nHeightTimes]);
            Command.ESC_t[2] = (byte) codepage;
            Command.ESC_M[2] = (byte) nFontType;
            if (codepage == 0) {
                return Other.byteArraysToBytes(new byte[][]{Command.GS_ExclamationMark, Command.ESC_t, Command.FS_and, Command.ESC_M, pbString});
            }
            return Other.byteArraysToBytes(new byte[][]{Command.GS_ExclamationMark, Command.ESC_t, Command.FS_dot, Command.ESC_M, pbString});
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }
printable.add(
            RawPrintable.Builder(
                PrinterCommand.POS_Print_Text(
                    "الأسم:\n",
                    ARABIC, 22, 0, 0, 0
                )
            ).build()
        )
    const val ARABIC = "ISO-8859-6"