Sending ZPL to Printers via cURL

A Quick Tip for Printing Labels

Shawn Roller

December 6, 2024

ProductivityGuideSuccessEfficiency

I’ve had several requests from clients to print labels en masse, so I thought maybe others will find my script useful.

This particular script takes a .csv file with headers of upc and msrp and a networked ZPL printer to send the labels to. We can send the ZPL payload directly to the printer IP at port 9100 to print it.

zpl-sender-upc.sh
1#!/bin/bash
2
3# Script to iterate through a csv file and send zpl to a printer IP address
4
5if [ $# -ne 2 ]; then
6    echo "Please provide CSV file and printer IP address as arguments"
7    exit 1
8fi
9
10CSV_FILE=$1
11PRINTER_IP=$2
12
13# ZPL template
14ZPL_TEMPLATE='^XA
15^PW600
16^LL400
17^FO40,10^BCN,100,Y,N,N
18^FD{{UPC}}^FS
19^FO40,150^A0N,40,40^FD$^FS
20^FO80,150^A0N,40,40^FD{{MSRP}}^FS
21^XZ'
22
23tail -n +2 "$CSV_FILE" | while IFS=, read -r upc msrp; do
24    upc=$(echo "$upc" | tr -d '"' | xargs)
25    msrp=$(echo "$msrp" | tr -d '"' | xargs)
26    
27    zpl=$(echo "$ZPL_TEMPLATE" | sed "s/{{UPC}}/$upc/g" | sed "s/{{MSRP}}/$msrp/g")
28    
29    echo "ZPL: $zpl"
30
31    # Post zpl to printer
32    if curl -s -m 3 -X POST -H "Content-Type: text/plain" --data "$zpl" "http://${PRINTER_IP}:9100/"; then
33        echo "Label printed successfully"
34    else
35        echo "Error printing label"
36    fi
37    
38    # short delay between labels
39    sleep 0.2
40done
41
42echo "Finished processing CSV file"
43

You can copy/paste that into your own .sh file, run chmod +x on the file, and then execute it like so: ./zpl-sender-upc.sh test.csv 1.1.1.1

Hopefully that will save somebody some time, even if it’s just my future self!