Printing to Zebra Printers from Windows using Python

This article provides Python code examples for Printing To Zebra Printers in a Windows environment. Utilizing Java libraries within Python, these scripts demonstrate how to send EPL (Eltron Programming Language) strings to Zebra printers for label and general printing tasks.

Python Code Examples for Zebra Printer Communication

The following Python functions leverage the javax.print and org.python.core.util libraries to facilitate communication with Zebra printers. These examples are designed for a Windows environment where Zebra printers are installed and accessible as print services.

Basic Print Job (Default Printer)

This function, CreatePrintJob(EPLString), sends an EPL string to the default printer configured in the Windows system. It looks up the default print service and creates a print job to send the provided EPL data.

def CreatePrintJob(EPLString):
    from javax.print import PrintService
    from javax.print import PrintServiceLookup
    from javax.print import SimpleDoc
    from javax.print import DocFlavor
    from org.python.core.util import StringUtil
    from jarray import array

    pserv = PrintServiceLookup.lookupDefaultPrintService()
    if pserv == None:
        print "ERROR-01: no default print service"

    data = StringUtil.toBytes(EPLString)
    doc = SimpleDoc(data, DocFlavor.BYTE_ARRAY.AUTOSENSE, None)
    pserv.createPrintJob().print(doc, None)
    return

This code snippet first imports necessary Java classes for print service lookup and job creation. It then retrieves the default print service using PrintServiceLookup.lookupDefaultPrintService(). The EPL string is converted to bytes, and a print job is created and executed, sending the EPL commands to the printer.

Listing Available Print Services

The LookupPrintServices() function is useful for identifying available print services on the Windows system. This can help in verifying if the Zebra printer is correctly recognized as a print service.

def LookupPrintServices():
    from javax.print import PrintService
    from javax.print import PrintServiceLookup
    from javax.print import SimpleDoc
    from javax.print import DocFlavor
    from jarray import array

    pservList = PrintServiceLookup.lookupPrintServices(None,None)
    if pservList == None:
        print "ERROR-01: no default print service"
    print pservList
    return

This function utilizes PrintServiceLookup.lookupPrintServices(None,None) to get a list of all available print services. It then prints this list to the console, allowing users to see the names of the printers recognized by the system. This is helpful for troubleshooting and ensuring the Zebra printer is listed.

Print Job to Specific Printer by Name

For printing to a specific Zebra printer, the CreateLabelPrintJob(EPLString,Name) function allows you to specify the printer name. This is crucial when multiple printers are connected, and you need to direct the print job to a particular Zebra printer.

def CreateLabelPrintJob(EPLString,Name):
    from javax.print import PrintService
    from javax.print import PrintServiceLookup
    from javax.print import SimpleDoc
    from javax.print import DocFlavor
    from org.python.core.util import StringUtil
    from jarray import array

    pservList = PrintServiceLookup.lookupPrintServices(None,None)
    if pservList == None:
        print "ERROR-01: no default print service"

    printer = None
    for i in range(len(pservList)):
        s = str(pservList[i])
        r = s.find(Name)
        if r != -1:
            printer = pservList[i]

    if printer == None:
        print "ERROR-01: no Label Printer"
        system.gui.messageBox("Printer " + Name + " not located, ensure correct printer is connected.","PRINTER NOT FOUND")
    else:
        data = StringUtil.toBytes(EPLString)
        doc = SimpleDoc(data, DocFlavor.BYTE_ARRAY.AUTOSENSE, None)
        printer.createPrintJob().print(doc, None)
    return

In this function, after retrieving the list of print services, the code iterates through them to find a printer whose name contains the provided Name parameter. If a printer with the specified name is found, the print job is directed to that printer. An error message is displayed if the printer is not found, ensuring robust handling of printer availability.

Conclusion

These Python code examples provide a foundation for printing to Zebra printers from Windows using EPL. They demonstrate how to send print jobs to default printers and specific named printers, as well as how to list available print services. These functions are valuable for automating label printing and other printing tasks in environments utilizing Zebra printers and Python scripting.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *