Skip to content

Instantly share code, notes, and snippets.

@djekl
Forked from tashian/README.md
Created June 28, 2016 10:36
Show Gist options
  • Select an option

  • Save djekl/4433f0b192a4a7114f51bd792dc46be6 to your computer and use it in GitHub Desktop.

Select an option

Save djekl/4433f0b192a4a7114f51bd792dc46be6 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