Skip to content

Instantly share code, notes, and snippets.

Last active December 4, 2020 17:52
Show Gist options
  • 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')
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']
lpr.on 'close', (code) ->
console.log "Child process exit with code #{code}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment