【发布时间】:2021-11-19 22:26:43
【问题描述】:
大家好,新来的,自学成才。我有一个代码,它接受表单输入并将其转换为文本块,以便我的热敏打印机可以打印标签。该脚本效果很好我正在尝试找出一种方法来打印到打印窗口(生成的 txt 代码),或者当我单击生成标签按钮时直接发送到 IP 为 10.100.2.200 的网络打印机。基本上减少了几步。谢谢
这是包含代码的页面
<!DOCTYPE html>
<html>
<head>
<title>Print Inventory Labels at Papa's</title>
<style>
* {
box-sizing: border-box;
}
div {
padding: 10px;
background-color: #f6f6f6;
overflow: hidden;
}
input[type=text], textarea, select {
font: 17px Calibri;
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
}
input[type=button]{
font: 17px Calibri;
width: auto;
float: right;
cursor: pointer;
padding: 7px;
}
.container{
width: 100%;
margin: 3.2rem auto 0 auto;
}
@media(min-width: 576px){
.container{
max-width: 540px;
}
}
@media(min-width: 768px){
.container{
max-width: 720px
}
</style>
</head>
<body>
<div>
<div class="container">
<div align="center">
<!--Add few elements to the form-->
<img src="https://www.papasjeepram.com/wp-content/uploads/2019/09/logo2.jpg" width="500" height="189">
</div>
<h2>Enter Values to Generate Stock Label</h2>
<div>
<input type="text" onkeyup="this.value = this.value.toUpperCase();"id="txtStock" placeholder="Stock Number" />
</div>
<div>
<input type="text" onkeyup="this.value = this.value.toUpperCase();"id="txtVin" maxlength="17" placeholder="Vin" />
</div>
<div>
<input type="text" onkeyup="this.value = this.value.toUpperCase();"id="txtYear" maxlength="4" placeholder="Year" />
</div>
<div>
<input type="text" onkeyup="this.value = this.value.toUpperCase();" id="txtMake" placeholder="Make" />
</div>
<div>
<input type="text" onkeyup="this.value = this.value.toUpperCase();" id="txtModel" placeholder="Model" />
</div>
<div>
<input type="text" onkeyup="this.value = this.value.toUpperCase();" id="txtColor" placeholder="Color" />
</div>
<div>
<div align="left">
<input type="button" id="bt" value="Generate Label" onclick="saveFile()" />
</div>
</div>
</div>
</body>
<script>
let saveFile = () => {
// Get the data from each element on the form.
const Stock = document.getElementById('txtStock');
const Vin = document.getElementById('txtVin');
const Year = document.getElementById('txtYear');
const Make = document.getElementById('txtMake');
const Model = document.getElementById('txtModel');
const Color = document.getElementById('txtColor');
// This variable stores all the data.
let data =
'\r^XA\n' +
'^A0,213\n' +
'^FO25,35^FD' + Stock.value + '^FS \r\n '+
'^BY3,2,225\n' +
'^FO45,220^BC^FD' + Vin.value + '^FS \r\n '+
'^A0,50\n' +
'^FO45,500^FD' + Year.value + ' ' + Make.value + ' ' + Model.value + ' ' + Color.value + ' ^FS \r\n '+
'^A0,55\n' +
'^FO435,630^FD' + Stock.value + '^FS \r\n '+
'^A0,50\n' +
'^FO275,630^FD' + Year.value + '^FS \r\n '+
'^A0,55\n' +
'^FO45,630^FD' + Stock.value + '^FS \r\n '+
'^A0,50\n' +
'^FO695,630^FD' + Year.value + '^FS \r\n '+
'^A0,55\n' +
'^FO435,835^FD' + Stock.value + '^FS \r\n '+
'^A0,50\n' +
'^FO275,835^FD' + Year.value + '^FS \r\n '+
'^A0,55\n' +
'^FO45,835^FD' + Stock.value + '^FS \r\n '+
'^A0,50\n' +
'^FO695,835^FD' + Year.value + '^FS \r\n '+
'^A0,30\n' +
'^FO45,705^FD' + Make.value + ' ' + Model.value + '^FS \r\n '+
'^A0,30\n' +
'^FO45,745^FD' + Vin.value + '^FS \r\n '+
'^A0,30\n' +
'^FO45,780^FD' + Color.value + '^FS \r\n '+
'^A0,30\n' +
'^FO435,705^FD' + Make.value + ' ' + Model.value + '^FS \r\n '+
'^A0,30\n' +
'^FO435,745^FD' + Vin.value + '^FS \r\n '+
'^A0,30\n' +
'^FO435,780^FD' + Color.value + '^FS \r\n '+
'^A0,30\n' +
'^FO40,900^FD' + Make.value + ' ' + Model.value + '^FS \r\n '+
'^A0,30\n' +
'^FO40,940^FD' + Vin.value + '^FS \r\n '+
'^A0,30\n' +
'^FO40,975^FD' + Color.value + '^FS \r\n '+
'^A0,30\n' +
'^FO435,900^FD' + Make.value + ' ' + Model.value + '^FS \r\n '+
'^A0,30\n' +
'^FO435,940^FD' + Vin.value + '^FS \r\n '+
'^A0,30\n' +
'^FO435,975^FD' + Color.value + '^FS \r\n '+
'^XZ';
// Convert the text to BLOB.
const textToBLOB = new Blob([data], { type: 'text/plain' });
const sFileName = 'Label.txt'; // The file to save the data.
let newLink = document.createElement("a");
newLink.print = sFileName;
if (window.webkitURL != null) {
newLink.href = window.webkitURL.createObjectURL(textToBLOB);
}
else {
newLink.href = window.URL.createObjectURL(textToBLOB);
newLink.style.display = "none";
document.body.appendChild(newLink);
}
newLink.click();
}
</script>
</html>
【问题讨论】:
-
请将相关代码部分直接添加到您的问题中。
-
嗨,贾斯汀,为了符合 SF,您应该在此页面中提供完整的工作 JavaScript 代码。
-
关于打印到远程打印机,如果您的打印机公开了一个打印服务器(我假设是这样),这是可能的。在这种情况下,您需要一个通过 HTTP POST 打开与打印机的连接的 Web 服务,将生成的文本作为正文发送...请参见此处 - stackoverflow.com/questions/470309/…
标签: javascript