Skip to content

Instantly share code, notes, and snippets.

@tashian
Last active December 4, 2020 17:52
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tashian/8985980 to your computer and use it in GitHub Desktop.
Save tashian/8985980 to your computer and use it in GitHub Desktop.
Just-in-time label printing on the Zebra ZP450 using node.js + Easypost + Pusher + cups

This is the script we use at yerdle to print labels from our Rails backend to our Zebra ZP450 printer.

the Zebra

See this blog post for the whole story.

dotenv = require('dotenv')
dotenv.load()
PusherClient = require('pusher-node-client').PusherClient
easypost = require('node-easypost')(process.env.EASYPOST_SECRET_KEY)
child_process = require('child_process')
fs = require('fs')
request = require('request')
pusher_client = new PusherClient
appId: process.env.PUSHER_APP_ID
key: process.env.PUSHER_KEY
secret: process.env.PUSHER_SECRET
pres = null
pusher_client.on 'connect', () ->
pres = pusher_client.subscribe("shipments")
pres.on 'new', (data) ->
console.log "shipment #{data.easypost_shipment_id} with tracking number #{data.tracking_code}"
easypost.Shipment.retrieve data.easypost_shipment_id, (err, shipment) ->
console.log("ERROR: #{err}") if err
shipment.label {file_format: 'zpl'}, (err, shipment) ->
console.log("ERROR: #{err}") if err
console.log "Fetching #{shipment.postage_label.label_zpl_url}"
lpr = child_process.spawn "lpr", ['-P', process.env.ZEBRA_PRINT_QUEUE_NAME, '-o', 'raw']
request(shipment.postage_label.label_zpl_url).pipe(lpr.stdin)
lpr.on 'close', (code) ->
console.log "Child process exit with code #{code}"
pusher_client.connect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment