-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (48 loc) · 1.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import {
Align, Model, Printer, Style, WebUSB,
} from 'escpos-buffer';
let printer;
const EPSON_VENDOR_ID = 1208;
const getOutput = () => document.getElementById('output').value;
async function setup(device) {
const model = new Model('TM-T20');
const connection = new WebUSB(device);
await connection.open();
return new Printer(model, connection);
}
async function getDevice() {
try {
// Re-setup previously allowed devices
let [device] = await navigator.usb.getDevices();
if (!device) {
// Request permission first time
device = await navigator.usb.requestDevice({
filters: [{
vendorId: EPSON_VENDOR_ID,
}],
});
}
return setup(device);
} catch (err) {
console.error(err);
return null;
}
}
async function connectAndPrint() {
try {
if (!printer) {
printer = await getDevice();
}
} catch (err) {
console.error(err);
}
printer.writeln('JACKSON COUNTY HOSPITAL', Style.DoubleHeight | Style.Bold, Align.Center);
printer.writeln('P.O. BOX 4- 01001', null, Align.Center);
printer.writeln('AMERICA', null, Align.Center);
printer.writeln('Tel No: 15552022, 29933392', null, Align.Center);
printer.writeln('[email protected]', null, Align.Center);
printer.writeln(getOutput());
printer.feed(6);
printer.cutter();
}
document.querySelector('button').addEventListener('click', connectAndPrint);