【问题标题】:Word wrap in cordova-plugin-datecs-printercordova-plugin-datecs-printer 中的自动换行
【发布时间】:2018-02-07 07:47:46
【问题描述】:

我正在使用 ionic 1 做一个移动应用程序,而对于在热敏打印机中打印,我正在使用 cordova-plugin-datecs-printer 插件。

除了自动换行之外,一切正常。打印机在行尾剪切了单词。 有没有办法调整或启用自动换行以避免单词不完整?

这是我的代码:

tale += '{center}{b}Le Petit Prince{/b}{br}{br}';
tale += '{left}Once when I was six years old I saw a magnificent picture in a book, called True Stories from Nature, about the primeval forest. It was a picture of a boa constrictor in the act of swallowing an animal. Here is a copy of the drawing.&n the book it said: "Boa constrictors swallow their prey whole, without chewing it. After that they are not able to move, and they sleep through the six months that they need for digestion.';


//Replace & with line breaks
var text1 = tale.replace(/&/g, '{br}{br}'); 
cordovaPrinter.printText(text1);

结果如下::(

【问题讨论】:

  • 这真的和ESC/POS无关,你应该删除那个标签,imo。
  • 根据插件创建者的建议:github.com/giorgiofellipe/…“请在 StackOverflow 上提出问题,提及 cordova-plugin-datecs-printer 并使用标签 cordova-plugins 和 escpos”
  • 这不是一个有用的建议。

标签: printing cordova-plugins escpos


【解决方案1】:

看看这个sn-p,我认为它是不言自明的。

var yourString = "The quick brown fox jumps over the lazy dog"; //replace with your string.
var finalString = "";
var maxLength = 16 // maximum number of characters to extract
var count = 0;
var maxTries = ((yourString.length / maxLength) * 2);

while (yourString.length > maxLength) {
  count++;
  if (count > maxTries) {
    break;
  }
  //trim the string to the maximum length
  var trimmedString = yourString.substr(0, maxLength);

  //re-trim if we are in the middle of a word
  trimmedString = trimmedString.substr(0, Math.min(trimmedString.length, trimmedString.lastIndexOf(" ")));

  yourString = yourString.replace(trimmedString + " ", "");

  finalString += trimmedString + '<br>';
}

finalString += yourString;

console.log(yourString);
console.log(finalString);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 2022-01-13
    • 2021-06-14
    相关资源
    最近更新 更多