Print characters with an acute in ZPL

亡梦爱人 提交于 2019-12-01 04:46:11

Use UTF-8 by placing a ^CI28 command at the top of your ZPL template, eg

^XA
^CI28
^CF0,80
^FO70,40^FDavión^FS
^XZ

If you copy and paste your first example into a text editor that can convert between UTF-8 and ANSI (Notepad++) you'll see that the first example is encoded as

^XA
^FO50,20
^CI7
^A0N,25,15
^FD
Amitié
^FS
^XZ

And this will be cause problems with your ZPL when rendered. See online examples for ANSI and UTF-8.

To fix this you could encode your values first (for example as hex and then prefix with ^FH)

According to the programing guide document from Zebra ^CI using 7 will get you Code Page 850 with some specific character replacements. When you say you had the file encoded in ANSI, I assume you mean Code Page Windows-1252 or ISO-8859-1 (latin1).

The character é in Windows-1252 and latin1 is #00E9, but that's Ú in 850; you would want #0082 for é in 850. Using ^CI7 you could apparently also get an é with #007B since that's one of the specific character replacements made with that command.

Using UTF8 (with ^CI28) is probably the way to go since it's widely supported and understood, but note that you could also try ^CI27 (which may work even if you have an older version of the Zebra firmware that doesn't support ^CI28) and that should get you code page 1252. If that doesn't work you'll need to encode your text using code page 850.

Here is what I did to be able to do that:

  • Define UTF-8 charset using ^CI28
  • Use Swiss unicode font. For my case I only needed to encode on a single line and I didn't want to change anything else on the document or printer settings. For that I used ^A@N,44,30,E:TT0003M_.TTF. If you want to define the font for the whole document check the first link below.
  • Prepared the string to recognize UTF-8 encoding with ^FH immediately before ^FD
  • Encoded the document to replace non-ASCII characters with their HEX representation:
    private static string ZebraEncode(string text)
    {
        var ret = new StringBuilder();

        var unicodeCharacterList = new Dictionary<char, string>();
        foreach(var ch in text)
        {
            if (!unicodeCharacterList.ContainsKey(ch))
            {
                var bytes = Encoding.UTF8.GetBytes(ch.ToString());
                if (bytes.Length > 1)
                {
                    var hexCode = string.Empty;
                    foreach(var b in bytes)
                    {
                        hexCode += $"_{BitConverter.ToString(new byte[] { b }).ToLower()}";
                    }

                    unicodeCharacterList[ch] = hexCode;
                }
                else
                    unicodeCharacterList[ch] = ch.ToString();

                ret.Append(unicodeCharacterList[ch]);
            }
            else
                ret.Append(unicodeCharacterList[ch]);
        };

        return ret.ToString();
    }

Info that I gathered in order to reach a solution:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!