【发布时间】:2021-10-11 19:31:55
【问题描述】:
我正在制作 POS 应用程序。我通过蓝牙串行连接我的应用程序以将数据发送到打印机。我的打印机是RPP02N 蓝牙热敏打印机。我正在使用Bluetooth Serial 插件。
这是我的代码:
await BluetoothSerial.clear();
await BluetoothSerial.disconnect();
let connection = await BluetoothSerial.connect(pId);
connection.subscribe(async success => {
if (success == 'OK') {
BluetoothSerial.write("Test Print").then(() => {
console.log("Print finished");
BluetoothSerial.disconnect();
});
} else {
BluetoothSerial.disconnect();
}
这很好用,但我必须使用raw string 来获取数据,如下所示:
" SHOP NAME "
" 77 ABC ST, 12345 "
" ----------------- "
" <MY_________DATA> "
很难像这样对齐内容。我需要我的内容具有响应性,它可以打印在不同的纸张尺寸上。它还需要能够打印图像标志。
我该怎么做?
转换成图片
我通过转换为图像尝试了另一种方法。
这是我的 html 内容:
<canvas>
<h3>Shop Name</h3>
<h5>77 ABC ST, 12345</h5>
<hr/>
<p>My content</p>
</canvas>
这是代码:
var img = new Image();
img.src = canvas.toDataURL("image/png");
这是在img.onLoad()方法里面:
img.onLoad = async() => {
....
....
const encoder = new EscPosEncoder();
let result = encoder
.image(img, 320, 160, 'atkinson', 128) //width must be multiplication of 8
.encode();
BluetoothSerial.write(result).then(() => {
console.log("Print finished");
BluetoothSerial.disconnect();
});
}
我正在使用this节点模块转换成ecs pos。
它不工作,打印机打印 1 个空行。
打印原始 html
我尝试打印原始 html。事实证明,它确实按字面意思打印原始 html(例如:<canvas>....</canvas>)。
【问题讨论】:
标签: javascript typescript ionic-framework printing bluetooth