间隙纸打印机,使用sdk打印。
原生写法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>LPAPIWeb Test</title>
<script type="text/javascript" src="./LPAPIWeb-1.0.1.min.js"></script>
<script>
var api = new LPAPIWeb();
/**
* 打开当前打印机;
*/
function openPrinter() {
var printers2 = api.getPrinters();
if (!printers2.length){
console.log('没有连接打印机。。')
return false;
}
var name = printers2[0].name;
var ip = printers2[0].ip;
return api.openPrinter({ip: ip, printerName: name});
}
function printQRCodeMore() {
// 打开打印机
if (!openPrinter())
return;
if (!api.startJob({width: 50, height: 20,orientation:0}))
return;
for (let i=1;i<=2;i++){
// 文字
api.drawText({
text: 'A-1-100' + i,
x: 17,
y: 8,
width: 30,
height: 5,
fontHeight: 10,
fontStyle: 1
});
// 二维码
api.draw2DQRCode({
text: '测试'+i,
x: 0,
y: 4,
width: 15
});
/* 批量打码精髓就在于两次调用api.commitJob, */
api.commitJob();
}
// 关闭打印机;
api.closePrinter();
}
/**
* 绘制二维码。
*/
function printQRCode() {
// 打开打印机
if (!openPrinter())
return;
if (!api.startJob({width: 50, height: 20,orientation:0}))
return;
// 文字
api.drawText({
text: 'A-1-1001',
x: 17,
y: 8,
width: 30,
height: 5,
fontHeight: 10,
fontStyle: 1
});
// 二维码
api.draw2DQRCode({
text: '1234567',
x: 0,
y: 4,
width: 15
});
api.commitJob();
// 关闭打印机;
api.closePrinter();
}
</script>
<style>
body {
margin: auto;
text-align: center;
background: #D1DF9F;
}
</style>
</head>
<body>
<div>
<div style="margin-top:300px"></div>
<input type="button" value="打码" onclick="printQRCode()">
<input type="button" value="批量打码(2)" onclick="printQRCodeMore()">
<input type="text">
</div>
</body>
</html>