后来我找到了https://github.com/qihang666/BluetoothPrinter这个项目,人家用蓝牙直接连打印机,比如我手头的DL-888AW就有蓝牙,我用usb为什么就不行呢,所以我就着手将这个项目中的代码拿过来。
尽管他里面的打印机指令集叫jprinter,但指令集实际上是TSPL,所以理论上应该可以复用。
拿过来的部分是https://github.com/qihang666/BluetoothPrinter/tree/master/components/gprint,
考虑到https://github.com/tojocky/node-printer/blob/master/examples/print_raw.js这里面提示的是data部分需要string类型,所以,我把tsc.js里面的command改成了字符串类型,把
jpPrinter.addCommand = function(content) {
// 将指令转成数组装起;
var code = new encode.TextEncoder("gb18030", {
NONSTANDARD_allowLegacyEncoding: true,
}).encode(content);
for (var i = 0; i < code.length; ++i) {
command.push(code[i]);
}
};
改成了
jpPrinter.addCommand = function(content) {
command = command + content;
};
然后打印的时候:
var command = tsc.jpPrinter.createNew();
console.log(command);
command.setSize(60, 40);
command.setGap(2);
command.setCls();
command.setText(50, 10, "2", 1, 1, "Hello");
command.setText(50, 100, "TSS24.BF2", 1, 1, "一二三");
// command.setQR(50, 50, "L", 5, "A", "977767937@qq.com");
command.setPagePrint();
var data=command.getData();
var jobid = "";
printer.printDirect({
data: data, // or simple String: "some text"
printer: "Deli_DL_888B_NEW_", // printer name, if missing then will print to default printer
type: "RAW", // type: RAW, TEXT, PDF, JPEG, COMMAND.. depends on platform
success: function(jobID) {
console.log("sent to printer with ID: " + jobID);
jobid = jobID;
},
error: function(err) {
console.log(err);
},
});
惊喜出现了,打印机工作了,但是汉字是乱码。