0

I'm doing a mobile app using ionic 1, and for printing in thermal printers I'm using the cordova-plugin-datecs-printer plugin.

Everything works ok except with the word wrapping. The printer cut words at the end of the line. Is there a way to adjust or enable word wrap to avoid incomplete words?

Here is my code:

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);

And this is the result: :(

enter image description here

GalloPinto
  • 667
  • 2
  • 11
  • 25
  • This has really nothing to do with ESC/POS, you should remove that tag, imo. – Marc Balmer Feb 02 '18 at 10:10
  • As suggested by the plugin creator: https://github.com/giorgiofellipe/cordova-plugin-datecs-printer#alignment-codes "Please ask question on StackOverflow mentioning cordova-plugin-datecs-printer and using the tags cordova-plugins and escpos" – GalloPinto Feb 02 '18 at 15:30
  • That is not a helpful advice. – Marc Balmer Feb 03 '18 at 13:22

1 Answers1

2

Take a look at this snippet, I think it's self explanatory.

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);
Giorgio Fellipe
  • 304
  • 1
  • 2
  • 10