Unicode isn't supported in the CPCL language. You can do it in ZPL though, and the iMZ supports ZPL. Check out this link.
Printing UTF-8 characters on bluetooth printer
-
24-06-2022 - |
Frage
I have an application where I should be able to print on a bluetooth printer, Zebra iMZ320, but I have some problems with UTF-8 specific characters (Æ, Ø or Å).
I am connecting to the device as follows:
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(macAddr);
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { Integer.TYPE });
bSocket = (BluetoothSocket)m.invoke(device, new Object[] { Integer.valueOf(1) });
bSocket.connect();
outStream = bSocket.getOutputStream();
inStream = bSocket.getInputStream();
After the socket is open, I am sending the data in CPCL:
String cpclData = "! U1 SETLP 5 2 24 \r\n"+text+"\r\n";
outStream.write(cpclData.getBytes());
outStream.flush();
But when I am trying to print the mentioned characters, it writes some abnormal characters instead.
I contacted Zebra, and one of their engineers wrote that I should try the following:
! 0 200 200 80 1
IN-MILLIMETERS
JOURNAL
CENTER
COUNTRY NORWAY
TEXT 4 0 0 8 COUNTRY IS NORWAY OR DENMARK
TEXT 4 0 0 15 Æ Ø Å
PRINT
But it does absolutely nothing.
Lösung 2
Andere Tipps
it is simple if you try to print a label from android device; when you write the data use "ISO-8859-1" encoding, look:
String cpclData = "! U1 SETLP 5 2 24 \r\n"+text+"\r\n";
outStream.write(EncodingUtils.getBytes(cpclData, "ISO-8859-1"));
outStream.flush();
In my case, it worked perfectly the solution provided by @jsanmarb but using the Code page 850 (Latin-1 - Western European languages) found here: https://www.ascii-codes.com/cp850.html
Found this solution here
public byte[] convertExtendedAscii(String input)
{
int length = input.length();
byte[] retVal = newbyte[length];
for(int i=0; i<length; i++)
{
char c = input.charAt(i);
if (c < 127)
{
retVal[i] = (byte)c;
}
else
{
retVal[i] = (byte)(c - 256);
}
}
return retVal;
}
In a similar problem, trying to print spanish special characters on a Zebra MZ220 printer via Bluetooth, finally I did the following (in this answer I add the characters Å, Æ, Ø, å, æ, ø) :
Define a piece of code that converts the objective string into the desired byte array:
public class Util { public final static String caracteresEspeciales = "ÜüÁáÉéÍíÓóÚúÑñÅÆØåæø"; public final static byte[] codigoCaracteresEspeciales = new byte[] {(byte) 0xDC, (byte) 0xFC, (byte) 0xC1, (byte) 0xE1, (byte) 0xC9, (byte) 0xE9, (byte) 0xCD, (byte) 0xED, (byte) 0xD3, (byte) 0xF3, (byte) 0xDA, (byte) 0xFA, (byte) 0xD1, (byte) 0xF1, (byte) 0xC5, (byte) 0xC6, (byte) 0xD8, (byte) 0xE5, (byte) 0xE6, (byte) 0xF8 }; public static byte[] stringABytes(String s) { int i, l, i_especial; byte b; byte[] b_arr; String s_sub; if(s == null) return null; if((l= s.length()) < 1) return new byte[0]; // convertimos a byte carácter por carácter b_arr= new byte[l]; for(i= 0; i < l; i++) { s_sub= s.substring(i, i + 1); i_especial= Util.caracteresEspeciales.indexOf(s_sub); if(i_especial < 0) b= (s_sub.getBytes())[0]; else b= Util.codigoCaracteresEspeciales[i_especial]; b_arr[i]= b; } return b_arr; } }
Those hexadecimal codes we can get them from PROMAN-CPCL document that coming whith the printer (APPENDIX C- CHARACTER TABLES, Latin 1 Character Set table).
Convert the string and send it.
String datos_cplc; byte[] b_arr; ... datos_cplc= "! 0 200 200 48 1\r\n" + "TEXT 7 0 0 0 12345678901234567890123456789012\r\n" + "TEXT 7 0 0 25 ÜüÁáÉéÍíÓóÚúÑñÅÆØåæø AEIOUaeiou1\r\n" + "FORM\r\n" + "PRINT\r\n"; b_arr= Util.stringABytes(datos_cplc); ... connection.write(b_arr);
The result: