# Printing items from a vison screen in to a Zebra printer string

**URL:** <https://forum.inductiveautomation.com/t/printing-items-from-a-vison-screen-in-to-a-zebra-printer-string/62947>\
**Category:** Ignition\
**Tags:** vision\
**Created:** [August 12, 2022, 9:34am UTC](https://forum.inductiveautomation.com/t/printing-items-from-a-vison-screen-in-to-a-zebra-printer-string/62947 "2022-08-12T09:34:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![batman01793](https://avatars.discourse-cdn.com/v4/letter/b/858c86/32.png) [@batman01793](https://forum.inductiveautomation.com/u/batman01793)\
**Post date:** [August 12, 2022, 9:34am UTC](https://forum.inductiveautomation.com/t/printing-items-from-a-vison-screen-in-to-a-zebra-printer-string/62947/1 "2022-08-12T09:34:44Z")

</div>

Hi All,  
I’m probably mising a simple line in my script here - so wanted to ask the hive mind for help.

I am trying to print a label based on information in a Vision form to a Zebra label printer.  
I have so far looked at the comments on here under the topic and I have structured some code -  
But it is printing the string of {root container etc… rather than the vaule within)

Please see my script below…

```auto
from java.net import Socket
from java.io import DataOutputStream

RollNum = "{Root Container.Form Container.Roll_no_Form Numeric Text Field.Roll_No_Numeric Text Field.intValue}"
width = "{Root Container.Form Container.Width_Form Numeric Text Field.Width_Numeric Text Field.intValue}"
Supplier = "{Root Container.Form Container.Form_Suoolier_drop_down.Supplier_Dropdown.selectedStringValue}"
SupplierRoll = "{Root Container.Form Container.SUPPLIER_PIECE_NO_Form Text Field.Supplier_Piece_No_Text Field.text}"
PurchaseOrder = "{Root Container.Form Container.PO Form Text Field.POText Field.text}"
Length = "{Root Container.Form Container.Length_Form Numeric Text Field.Length_Numeric Text Field.floatValue}"

strMessage100 ="^XA"
StrMessage110 ="^CF0,40"
StrMessage111 ="^FO35,50^FD%s^FS" % RollNum
strMessage112 ="^CF0,30"
StrMessage113 ="^FO30,115^FDWIDTH:%s ^FS" % width
StrMessage114 = "^FO30,155^FD%s ^FS" % RollNum
StrMessage115 = "^BY5,1,45"
StrMessage116 = "^FO35,200^BC^FDROLL %s^FS" % RollNum
StrMessage117 = "^FO30,290^FDSUPPLIER:%s ^FS" % Supplier
StrMessage118 = "^FO30,330^FDPURCHASE ORDER: %s ^FS" % PurchaseOrder
StrMessage119 = "^FO30,370^FDLENGTH:%s ^FS" % Length
StrMessage120 = "^XZ"

strOut = strMessage100+StrMessage110+StrMessage111+strMessage112+StrMessage113+StrMessage114+StrMessage115+StrMessage116+StrMessage117+StrMessage118+StrMessage119+StrMessage120
  
  
printerIP='172.016.007.133'
port=9100
 
try:
  # Open Socket Connection
  clientSocket=Socket(printerIP,port)
  #Open data output stream
  outToPrinter=DataOutputStream(clientSocket.getOutputStream())
  #Send Data to Printer
  outToPrinter.write(strOut)
  #close data stream and socket
  outToPrinter.close();
  clientSocket.close();
except IOError:
  print "Error", row

```

Any help and pointers for an absolute beginner would be great 🙂

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 12, 2022, 11:19am UTC](https://forum.inductiveautomation.com/t/printing-items-from-a-vison-screen-in-to-a-zebra-printer-string/62947/2 "2022-08-12T11:19:50Z")

</div>

Two things.

- Your value sources are in quotes. That makes it a literal string.
- Looks like you’re mixing scripting stuff with expression stuff. Instead of typing in the curly braces, use the property browser.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/b/db518bc202f8716120a7129ec054f03c4b224d1c.png)

---

<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:** [August 12, 2022, 11:52am UTC](https://forum.inductiveautomation.com/t/printing-items-from-a-vison-screen-in-to-a-zebra-printer-string/62947/3 "2022-08-12T11:52:48Z")

</div>

Also, please **edit** your post to place your code in a formatted block. (Highlight the code then click the \</\> button.)

---

<div class="post-metadata">

**Author:** ![batman01793](https://avatars.discourse-cdn.com/v4/letter/b/858c86/32.png) [@batman01793](https://forum.inductiveautomation.com/u/batman01793)\
**Post date:** [August 12, 2022, 2:30pm UTC](https://forum.inductiveautomation.com/t/printing-items-from-a-vison-screen-in-to-a-zebra-printer-string/62947/4 "2022-08-12T14:30:53Z")

</div>

Thanks - but I am writing the script as a script & I don’t see the property and flag browser buttons

see photo of screenshot

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/0/c0424a9491299d6f54b3c7455aea6656b279de81.jpeg)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 12, 2022, 3:35pm UTC](https://forum.inductiveautomation.com/t/printing-items-from-a-vison-screen-in-to-a-zebra-printer-string/62947/5 "2022-08-12T15:35:53Z")

</div>

Gotcha. I would make the script a function and pass your values into the function.

```python
def printLabel(RollNum, width, Supplier, SupplierRoll, PurchaseOrder, Length):
	from java.net import Socket
	from java.io import DataOutputStream

	strMessage100 ="^XA"
	StrMessage110 ="^CF0,40"
	StrMessage111 ="^FO35,50^FD%s^FS" % RollNum
	strMessage112 ="^CF0,30"
	StrMessage113 ="^FO30,115^FDWIDTH:%s ^FS" % width
	StrMessage114 = "^FO30,155^FD%s ^FS" % RollNum
	StrMessage115 = "^BY5,1,45"
	StrMessage116 = "^FO35,200^BC^FDROLL %s^FS" % RollNum
	StrMessage117 = "^FO30,290^FDSUPPLIER:%s ^FS" % Supplier
	StrMessage118 = "^FO30,330^FDPURCHASE ORDER: %s ^FS" % PurchaseOrder
	StrMessage119 = "^FO30,370^FDLENGTH:%s ^FS" % Length
	StrMessage120 = "^XZ"

	strOut = strMessage100+StrMessage110+StrMessage111+strMessage112+StrMessage113+StrMessage114+StrMessage115+StrMessage116+StrMessage117+StrMessage118+StrMessage119+StrMessage120
	  
	  
	printerIP='172.016.007.133'
	port=9100
	 
	try:
	  # Open Socket Connection
	  clientSocket=Socket(printerIP,port)
	  #Open data output stream
	  outToPrinter=DataOutputStream(clientSocket.getOutputStream())
	  #Send Data to Printer
	  outToPrinter.write(strOut)
	  #close data stream and socket
	  outToPrinter.close();
	  clientSocket.close();
	except IOError:
	  print "Error", row

```

Then, (assuming you’re calling this from a button), it would look something like this.

```python
RollNum = event.source.parent.getComponent("Roll_No_Numeric Text Field").intValue
width = event.source.parent.getComponent("Width_Numeric Text Field").intValue
Supplier = event.source.parent.getComponent("Supplier_Dropdown").selectedStringValue
SupplierRoll = event.source.parent.getComponent("Supplier_Piece_No_Text Field").text
PurchaseOrder = event.source.parent.getComponent("POText Field").text
Length = event.source.parent.getComponent("Length_Numeric Text Field").floatValue

PRINT_GI_LABEL_SCRIPT.printLabel(RollNum, wodth, Supplier, SupplierRoll, PruchaseOrder, Length)

```

However you call it from the window, _that_ editor will let you browse the property paths.

---

<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:** [August 12, 2022, 3:41pm UTC](https://forum.inductiveautomation.com/t/printing-items-from-a-vison-screen-in-to-a-zebra-printer-string/62947/6 "2022-08-12T15:41:18Z")

</div>

You’re writing a project _library_ script. You could potentially call this script from anywhere (Vision, Perspective, an alarm notification pipeline, etc), so even if the curly brace syntax works (which, again, it doesn’t in scripts), there would be no ‘context’ to know _which_ `Root Container` to use.

Also, Jordan already beat me to the ‘make it a function’ advice, but one other improvement is that you can use a multiline string and new-style formatting to make things a bit more readable (in my opinion):

```python
def print_gi_label_script(rollnum, width, supplier, supplierRoll, purchaseOrder, length):
	strOut = """
	^XA
	^FO35,50^FD{rollnum}^FS
	^CF0,30
	^FO30,115^FDWIDTH:{width} ^FS
	^FO30,155^FD{rollnum} ^FS
	^BY5,1,45
	^FO35,200^BC^FDROLL {rollnum}^FS
	^FO30,290^FDSUPPLIER:{supplier} ^FS
	^FO30,330^FDPURCHASE ORDER: {purchaseOrder} ^FS
	^FO30,370^FDLENGTH:{length} ^FS
	^XZ
	""".format(rollnum=rollnum, width=width, supplier=supplier, purchaseOrder=purchaseOrder, length=length)	

```

---

<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:** [August 12, 2022, 4:29pm UTC](https://forum.inductiveautomation.com/t/printing-items-from-a-vison-screen-in-to-a-zebra-printer-string/62947/7 "2022-08-12T16:29:47Z")

</div>

On a side note, it is MUCH easier to just install the zebra as a printer on the Gateway machine and print reports to it. Design your label as a report and then print it using system.report.execute.

This does away with the need to maintain templates on the printer and to use ZPL language too.

---

<div class="post-metadata">

**Author:** ![Dustin\_VanKeuren](https://avatars.discourse-cdn.com/v4/letter/d/b782af/32.png) [@Dustin\_VanKeuren](https://forum.inductiveautomation.com/u/Dustin_VanKeuren)\
**Post date:** [September 11, 2024, 11:54am UTC](https://forum.inductiveautomation.com/t/printing-items-from-a-vison-screen-in-to-a-zebra-printer-string/62947/8 "2024-09-11T11:54:17Z")

</div>

That is assuming they purchased the reporting module.
