ZPL Code not displaying properly

I have the following ZPL code:

^XZ
^XA
^MMT
^PW900
^LL300
^LS0
^FT900,132^A0I,150,150^FB854,1,38,C^FH\^CI28^FDABC#1\5C&^FS^CI27
^BY4,3,60^FT724,31^BCI,,N,N
^FH\^FD>:FREEZER#1^FS
^PQ1,0,1,Y
^XZ

which works fine if you paste it here : https://labelary.com/viewer.html

However, I am trying to store it in a variable in my code:

let prepareData = "^XZ"+
                      "^XA"+
                      "^MMT"+
                      "^PW900"+
                      "^LL300"+
                      "^LS0"+
                      "^FT900,132^A0I,150,150^FB854,1,38,C^FH\^CI28^FDNBA#1\5C&^FS^CI27"+
                      "^BY4,3,60^FT724,31^BCI,,N,N"+
                      "^FH\^FD>:FREEZER#1^FS"+
                      "^PQ1,0,1,Y"+
                      "^XZ"

JSFiddle here : https://jsfiddle.net/x8c14tpn/

It seems to be printing in console some weird characters which I think I should escape but I’m afraid if escaping it would mess up the code. So is there a way to keep it as it is?

Visual Studio was telling me Octal escape sequences are not allowed. Use the syntax '\x05' for the \5 on this line "^FT900,132^A0I,150,150^FB854,1,38,C^FH\^CI28^FDNBA#1\5C&^FS^CI27"+. Even though I replaced it with \x05 it didn’t print as expected.

Is it because carriage returns and line feeds are missing?

I’ve tried pasting everything in one line as well and it still behaves in similar manner.

Put the string in backticks instead of double quotes.

It appears that if you use a template string you need to escape the slashes (\) e.g.

`
^XZ
^XA
^MMT
^PW900
^LL300
^LS0
^FT900,132^A0I,150,150^FB854,1,38,C^FH\\^CI28^FDABC#1\\5C&^FS^CI27
^BY4,3,60^FT724,31^BCI,,N,N
^FH\\^FD>:FREEZER#1^FS
^PQ1,0,1,Y
^XZ
`

Otherwise it throws an error: octal escape sequences are not allowed in template strings

Strange but in that case you might have also not the wanted result as backslashes are normally not escaped in template strings or?

1 Like

Well if you console.log it out in the browser it does appear to give the desired result e.g.

^XZ
^XA
^MMT
^PW900
^LL300
^LS0
^FT900,132^A0I,150,150^FB854,1,38,C^FH\^CI28^FDABC#1\5C&^FS^CI27
^BY4,3,60^FT724,31^BCI,,N,N
^FH\^FD>:FREEZER#1^FS
^PQ1,0,1,Y
^XZ

Here it’s showing like this:

Here’s more enlarged image:

Hmm, it seems to be working in this case: