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

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…

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 :slight_smile:

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.

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

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

see photo of screenshot

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

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.

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.

2 Likes

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):

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)	
1 Like

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.

2 Likes

That is assuming they purchased the reporting module.