-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
49 lines (33 loc) · 1.1 KB
/
index.ts
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
import { Printer, Image, BitmapDensity } from "@node-escpos/core";
import USB from "@node-escpos/usb-adapter";
(async () => {
console.log("It's printing time!");
const image = await Image.load("./blessed.jpg");
console.log("Image loaded, size:", image.size);
const device = new USB();
console.log("USB Device created...");
device.open(async (err) => {
if (err) {
console.error("Error opening device:", err);
return;
}
console.log("Device opened successfully.");
const printer = new Printer(device, { encoding: "GB18030" });
await imageWithLineSpacing(printer, image, "s8");
printer
.feed()
.close();
});
device.close();
})();
async function imageWithLineSpacing(printer: Printer<[]>, image: Image, density?: BitmapDensity | undefined) {
const defaultLineSpace = printer.lineSpace;
const lineSpace24 = (n?: number | null) => {
printer.buffer.write("\x1B\x33");
printer.buffer.writeUInt8(24);
return printer;
}
printer.lineSpace = lineSpace24;
await printer.image(image, density);
printer.lineSpace = defaultLineSpace;
}