问题:我使用qzTray打印收据,之前我使用的是html格式,但打印质量不好。所以我转向 JavaScript 并编写了一堆函数来打印收据,我使用这个函数在每行打印 42 个字符,我有一个 105 个字符的字符串,该字符串将打印 3 行。我写了一些代码,但它不能完全工作,谁能给我一个提示如何做到这一点?
第一行打印得很好,但从第二行开始,substr 看起来好像不起作用,我不知道为什么。
谢谢是提前。
window.const_alignLeft = '\x1B\x61\x30';
window.const_newLine = '\x0A';
function getItemRowFull(itemTitle, itemPrice = null) {
var totalColsPerPage = 42;
var leftCols = 35;
var marginSpace = totalColsPerPage === 48 ? '' : ' ';
if(itemTitle.length > leftCols) {
leftCols = (!itemPrice) ? totalColsPerPage : leftCols;
let itemTitleLength = itemTitle.length;
let receipt = '';
let startFrom = 0;
let remainingCharacters = itemTitle.length;
while (startFrom < itemTitleLength) {
receipt += const_alignLeft + marginSpace + itemTitle.substr(startFrom, startFrom + leftCols);
receipt += const_newLine;
remainingCharacters = (remainingCharacters + itemTitleLength) - (itemTitleLength + leftCols);
startFrom = (startFrom + leftCols);
}
return receipt;
}
}
Image在下图中,该功能将用于与注释相关的段落。