# Printing Barcode Label

**URL:** <https://forum.inductiveautomation.com/t/printing-barcode-label/20914>\
**Category:** Ignition\
**Tags:** ignition79\
**Created:** [November 16, 2018, 10:23pm UTC](https://forum.inductiveautomation.com/t/printing-barcode-label/20914 "2018-11-16T22:23:00Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam.Naji](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sam.naji/32/4657_2.png) [@Sam.Naji](https://forum.inductiveautomation.com/u/Sam.Naji)\
**Post date:** [November 16, 2018, 10:23pm UTC](https://forum.inductiveautomation.com/t/printing-barcode-label/20914/1 "2018-11-16T22:23:00Z")

</div>

I have Zebra PC43d. I am trying to print the attached label: ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/f/fdada67346c3f81a7205e13ab1e61e361b5b7b50.jpg)

I used a report component with a print button. ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/5/509ac1fd1a8fc83865adcaf0a1d9693002f61b76.jpg)

I am not familiar with ZPL but I start using in this Print button. Here is the script:

import socket  
HOST = ‘10.81.19.189’  
PORT = 9100  
s = socket.socket(socket.AF\_INET, socket.SOCK\_STREAM)  
s.connect((HOST, PORT))  
s.sendall(’^XA’)  
s.sendall(’^FO50,50^ADN,36,20^FLine 50 50’)  
s.sendall(’^FS’)  
s.sendall(’^FO60,50^ADN,36,20^FColor60 50’)  
s.sendall(’^FS’)  
s.sendall(’^FO50,80^ADN,36,20^FDLot 50 80’)  
s.sendall(’^FS’)  
s.sendall(’^FO50,110^ADN,36,20^FDTubeJob#50 110’)  
s.sendall(’^FS’)  
s.sendall(’^FO50,150^ADN,36,20^FDQuantity:50 150’)  
s.sendall(’^FS’)  
s.sendall(’^XZ’)  
s.close()

It is printing but I don’t have any values from my database that is driving the label. Could someone tell me how to associate the data that is on the report (query Source) to get it to show on the label?  
The script above is showing just Label. For example: ^FO50,150^ADN,36,20^FDQuantity:50 150’ it shows quantity name but I don’t know how to bring the value in.

I appreciate your help.

Thanks…

---

<div class="post-metadata">

**Author:** ![MMaynard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmaynard/32/3366_2.png) [@MMaynard](https://forum.inductiveautomation.com/u/MMaynard)\
**Post date:** [November 16, 2018, 10:29pm UTC](https://forum.inductiveautomation.com/t/printing-barcode-label/20914/2 "2018-11-16T22:29:01Z")

</div>

If you want to use ZPL to do it you will have to concatenate the strings to pass in the ZPL.  
Something like

> import socket  
> HOST = '10.81.19.189'  
> PORT = 9100  
> s = socket.socket(socket.AF\_INET, socket.SOCK\_STREAM)  
> s.connect((HOST, PORT))  
> s.sendall('^XA')  
> s.sendall('^FO50,50^ADN,36,20^FLine 50 50')  
> s.sendall('^FS')  
> s.sendall('^FO60,50^ADN,36,20^FColor60 50')  
> s.sendall('^FS')  
> s.sendall('^FO50,80^ADN,36,20^FDLot 50 80')  
> s.sendall('^FS')  
> FDTubeJob = system.tag.read('TubeJobTag').value  
> s.sendall('^FO50,110^ADN,36,20^'+FDTubeJob+'#50 110')  
> s.sendall('^FS')  
> FDQuanitity = system.tag.read('FDQuantity').value  
> s.sendall('^FO50,150^ADN,36,20^'+FDQuantity+':50 150')  
> s.sendall('^FS')  
> s.sendall('^XZ')  
> s.close()

Or you can install the printer on the gateway and print to it using  
system.report.executeAndDistribute  
I have numerous systems printing a few thousand labels a day to all different versions of zebras using the execute method.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 16, 2018, 10:43pm UTC](https://forum.inductiveautomation.com/t/printing-barcode-label/20914/3 "2018-11-16T22:43:18Z")

</div>

Totally personal preference, but since ZPL is already such an obscure format I like using triple-quoted strings to keep it separate from code:  
`<better example below>`

---

<div class="post-metadata">

**Author:** ![Sam.Naji](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sam.naji/32/4657_2.png) [@Sam.Naji](https://forum.inductiveautomation.com/u/Sam.Naji)\
**Post date:** [November 17, 2018, 12:46am UTC](https://forum.inductiveautomation.com/t/printing-barcode-label/20914/4 "2018-11-17T00:46:19Z")

</div>

Thank you for the info. My preference is not to use the ZPL since I have to deal with a third party utility.

You mentioned that I can install the printer on the Gateway and print to it using system.report.executeAndDistribute. Can you provide an example with a little more detail.Thank you!  
My situation is I have jobs running on 6 different lines. each line has a Zebra barcode printer. As soon the job is completed, a FinishGood label is printed on the printer that is associated with the line. I am using a report that gets its data from several querirs and put them as a record with 8 fields. The values of these fields are the data I want to print on label. .

---

<div class="post-metadata">

**Author:** ![Sam.Naji](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sam.naji/32/4657_2.png) [@Sam.Naji](https://forum.inductiveautomation.com/u/Sam.Naji)\
**Post date:** [November 17, 2018, 12:54am UTC](https://forum.inductiveautomation.com/t/printing-barcode-label/20914/5 "2018-11-17T00:54:51Z")

</div>

Thank you P. Could you be more specific on how to use what you indicated above in the grand schema of the entire code I listed early. For example, do I put this:

import socket  
HOST = ‘10.81.19.189’  
PORT = 9100  
s = socket.socket(socket.AF\_INET, socket.SOCK\_STREAM)  
s.connect((HOST, PORT))

before your code above. Can you provide me with an example? Thanks…

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 17, 2018, 1:00am UTC](https://forum.inductiveautomation.com/t/printing-barcode-label/20914/6 "2018-11-17T01:00:10Z")

</div>

Put together, the code would look like this - basically, create the string and send it to the printer all at once, rather than a lot of individual sendall() calls.

```auto
import socket
HOST = '10.81.19.189'
PORT = 9100
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

label = """
^XA
^FO50,50^ADN,36,20^FLine 50 50
^FS
^FO60,50^ADN,36,20^FColor60 50
^FS
^FO50,80^ADN,36,20^FDLot 50 80
^FS
^FO50,110^ADN,36,20^FD%(TubeJob)i#50 110
^FS
^FO50,150^ADN,36,20^FD%(Quantity)i:50 150
^FS
^XZ
""" % {
	'TubeJob': system.tag.read('TubeJobTag').value, 
	'Quantity': system.tag.read('FDQuantity').value
}

s.sendall(label)
s.close()

```

---

<div class="post-metadata">

**Author:** ![Sam.Naji](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sam.naji/32/4657_2.png) [@Sam.Naji](https://forum.inductiveautomation.com/u/Sam.Naji)\
**Post date:** [November 17, 2018, 1:34am UTC](https://forum.inductiveautomation.com/t/printing-barcode-label/20914/7 "2018-11-17T01:34:47Z")

</div>

> [@paul-griffith](#):
>
> import socket  
> HOST = '10.81.19.189'  
> PORT = 9100  
> s = socket.socket(socket.AF\_INET, socket.SOCK\_STREAM)  
> s.connect((HOST, PORT))
> 
> label = """  
> ^XA  
> ^FO50,50^ADN,36,20^FLine 50 50  
> ^FS  
> ^FO60,50^ADN,36,20^FColor60 50  
> ^FS  
> ^FO50,80^ADN,36,20^FDLot 50 80  
> ^FS  
> ^FO50,110^ADN,36,20^FD%(TubeJob)#50 110  
> ^FS  
> ^FO50,150^ADN,36,20^FD%(Quantity):50 150  
> ^FS  
> ^XZ  
> """ % {  
> 'TubeJob': system.tag.read('TubeJobTag').value,  
> 'Quantity': system.tag.read('FDQuantity').value  
> }
> 
> s.sendall(label)  
> s.close()

**I am getting an error on line 7 (label = """)**

_Traceback (most recent call last):_  
\_ File "event:actionPerformed", line 7, in \_  
_ValueError: unsupported format character ' ' (0x20) at index 145_

\_ at org.python.core.Py.ValueError(Py.java:309)\_  
\_ \_

_Ignition v7.9.9 (b2018081621)_  
_Java: Oracle Corporation 1.8.0\_172_

Here is the code I used:  
import socket  
HOST = '192.168.0.20'  
PORT = 631  
s = socket.socket(socket.AF\_INET, socket.SOCK\_STREAM)  
s.connect((HOST, PORT))

_label = """_  
^XA  
^FO50,50^ADN,36,20^FLine 50 50  
^FS  
^FO60,50^ADN,36,20^FColor60 50  
^FS  
^FO50,80^ADN,36,20^FDLot 50 80  
^FS  
^FO50,110^ADN,36,20^FD%(TubeJob)#50 110  
^FS  
^FO50,150^ADN,36,20^FD%(Quantity):50 150  
^FS  
^XZ  
""" % {  
'TubeJob': system.tag.read('TubeJobTag').value,  
'Quantity': system.tag.read('FDQuantity').value  
}

s.sendall(label)  
s.close()

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [November 19, 2018, 9:39am UTC](https://forum.inductiveautomation.com/t/printing-barcode-label/20914/8 "2018-11-19T09:39:13Z")

</div>

There’s a small bug in the script. When using `%`-interpolation, you should also denote the datatype you want (number, string, date, …). That’s done as `%(variable)s` for string `%(variable)i` for integer, etc.

So here, if both are integers, you should use `%(TubeJob)i` and `%(Quantity)i`. Combined, that gives the following code:

```auto
import socket
HOST = '10.81.19.189'
PORT = 9100
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

label = """
^XA
^FO50,50^ADN,36,20^FLine 50 50
^FS
^FO60,50^ADN,36,20^FColor60 50
^FS
^FO50,80^ADN,36,20^FDLot 50 80
^FS
^FO50,110^ADN,36,20^FD%(TubeJob)i#50 110
^FS
^FO50,150^ADN,36,20^FD%(Quantity)i:50 150
^FS
^XZ
""" % {
	'TubeJob': system.tag.read('TubeJobTag').value, 
	'Quantity': system.tag.read('FDQuantity').value
}

s.sendall(label)
s.close()

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 19, 2018, 4:06pm UTC](https://forum.inductiveautomation.com/t/printing-barcode-label/20914/9 "2018-11-19T16:06:51Z")

</div>

> [@Sanderd17](#):
>
> There's a small bug in the script. When using %-interpolation, you should also denote the datatype you want (number, string, date, ...). That's done as %(variable)s for string %(variable)i for integer, etc.

Good catch - that's what I get for posting totally untested code.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 19, 2018, 4:39pm UTC](https://forum.inductiveautomation.com/t/printing-barcode-label/20914/10 "2018-11-19T16:39:44Z")

</div>

> [@paul-griffith](#):
>
> Good catch - that's what I get for posting totally untested code.

Don't feel bad. There's lots of untested code posted around here, including by me. Buyer beware!

---

<div class="post-metadata">

**Author:** ![Sam.Naji](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sam.naji/32/4657_2.png) [@Sam.Naji](https://forum.inductiveautomation.com/u/Sam.Naji)\
**Post date:** [November 19, 2018, 10:03pm UTC](https://forum.inductiveautomation.com/t/printing-barcode-label/20914/11 "2018-11-19T22:03:48Z")

</div>

Thank you for your feedback.

---

<div class="post-metadata">

**Author:** ![Sam.Naji](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sam.naji/32/4657_2.png) [@Sam.Naji](https://forum.inductiveautomation.com/u/Sam.Naji)\
**Post date:** [November 19, 2018, 10:04pm UTC](https://forum.inductiveautomation.com/t/printing-barcode-label/20914/12 "2018-11-19T22:04:30Z")

</div>

Thank you all for your help - You guys are awesome.  
Sam

---

<div class="post-metadata">

**Author:** ![Paullys50](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paullys50/32/3324_2.png) [@Paullys50](https://forum.inductiveautomation.com/u/Paullys50)\
**Post date:** [November 20, 2018, 2:28am UTC](https://forum.inductiveautomation.com/t/printing-barcode-label/20914/13 "2018-11-20T02:28:34Z")

</div>

See this [thread](http://forum.inductiveautomation.com/t/send-zpl-command-to-zebra-170pax4-via-tcp-ip-socket/6003/14) for another possible direct ZPL option, and I’m going to steal ryanjmclaughlin’s example as I’ve used it to print to zebra’s as also.

postData is the ZPL string.

`system.net.httpPost('http://192.168.1.10/pstprnt', postData="^XA^WD^XZ")`
