Created
May 21, 2018 21:19
-
-
Save lmccart/b2fb43c686df47490517d9a08f69ae3b to your computer and use it in GitHub Desktop.
printer-example.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/song940/node-escpos | |
var path = require('path'); | |
var express = require('express'); | |
var app = express(); | |
var http = require('http').Server(app); | |
var io = require('socket.io')(http); | |
var escpos = require('escpos'); | |
process.on('uncaughtException', function (err) { | |
console.error(err); | |
console.log("Node NOT Exiting..."); | |
}); | |
try { | |
var device = new escpos.USB(); | |
var printer = new escpos.Printer(device); | |
} catch(e) { | |
console.log(e); | |
} | |
app.use(express.static(path.join(__dirname, 'public'))); | |
io.on('connection', function(socket){ | |
socket.on('print', function(data){ | |
print(data); | |
}); | |
// forward all messages to clients | |
socket.on('message', function(data){ | |
socket.broadcast.emit('message', data); | |
}); | |
socket.on('line', function(data){ | |
socket.broadcast.emit('line', data); | |
}); | |
}); | |
http.listen(3000, function(){ | |
console.log('listening on *:3000'); | |
}); | |
function print(data) { | |
if (printer) { | |
escpos.Image.load(__dirname + '/cocktail3.png', function(image){ | |
device.open(function(){ | |
printer.font('a'); | |
printer.size(1, 1); | |
printer.align('CT'); | |
printer.text('24h HOST'); | |
printer.text('\n'); | |
printer.image(image, 'd24'); | |
printer.text('\n'); | |
// .image(image, 's8') | |
// .image(image, 'd8') | |
// .image(image, 's24') | |
//.raster(image) | |
// .raster(image, 'dw') | |
// .raster(image, 'dh') | |
// .raster(image, 'dwdh') | |
printer.align('LT'); | |
if (data) { | |
printer.text(data); | |
} | |
printer.text('\n\n') | |
printer.cut(); | |
printer.close(); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment