Search code examples
c#zplupszpl-ii

How to deal with special characters when using the UPS Shipping API


I'm trying to print international shipment labels using the UPS Shipping API. The response from the API is base64 encoded. This is what is used to decode the response base64 encoded zpl.

string ZPL = Encoding.UTF8.GetString(Convert.FromBase64String(graphicImage));

The problem is some of the recipient information contains Spanish Characters such as the é in "José". In the response ZPLII code, all of these characters will be replaced by a replacement characters �, making the name and addresses hardly readable.

I've found a solution for printing Unicode characters in ZPL, where I replace ^CI27to ^CI28, all ^FD to ^FH^FD, all ^FV to ^FH^FV. However, since all the Spanish characters are replaced into �, I can't target the special characters to map them into UTF8 Hex Code correctly such as mapping é to _C3_A9.

I'm thinking of removing all accents such as replacing all é to e in advance, but I'm not sure if the modified words still have the same meaning in Spanish.

Here is an example response ZPL before modified:

^FO15,142^A0N,28,32^FVSHIP TO: ^FS
^FO61,166^A0N,28,32^FVJOS�^FS
^FO61,194^A0N,28,32^FV(123)456-9890^FS
^FO61,222^A0N,28,32^FVSomeAddress with �^FS
^FO61,251^A0N,28,32^FVAPT 1, D^FS
^FO61,279^A0N,45,44^FV12345  MONTERREY  NL^FS
^FO61,324^A0N,45,44^FVCOUNTRY^FS

Are there any suggestions?


Solution

  • I am not sure whether this is a ZPL related issue. To me this looks like a problem with the encoding of the input file. I had a similar problem with characters from the Slovakian language. Instead of "ľ,š,č,ť,ž,ý,á,í,é,..." characters there were "�" characters in the whole file. The file itself was reporting as saved in UTF-8 encoding, which should fully support the Slovakian characters. I tried to open this file with Windows 1250 (Central European) encoding - that removed all "�" characters. Then I saved the file with UTF-8 and the problem was fixed.

    So I think you should try to play a little bit with the encoding of the input characters and get them to UTF-8 encoding. Maybe they are reporting as UTF-8 characters, as they did in my case, but just for a sanity check, try to reopen them in Spanish encoding (should be 1252) and save them as UTF-8 to see if it changes anything.

    However, in order to print these characters using ZPL you have to change the character set in the ZPL using the ^CI command with a value of 28 for Unicode UTF-8 encoding.

    Try this in the Labelary Online ZPL viewer :

    ^CI28
    ^FO15,142^A0N,28,32^FVSHIP TO: ^FS
    ^FO61,166^A0N,28,32^FVJOSÉ áíéóúñü^FS
    ^FO61,194^A0N,28,32^FV(123)456-9890^FS
    ^FO61,222^A0N,28,32^FVSomeAddress with íéá^FS
    ^FO61,251^A0N,28,32^FVAPT 1, D^FS
    ^FO61,279^A0N,45,44^FV12345  MONTERREY  NL^FS
    ^FO61,324^A0N,45,44^FVCOUNTRY^FS
    

    I hope I helped you. Have a nice day :)