razmans / ionicBluetoothPrint

If you are looking to find out how to print on a bluetooth printer with an ionic 2+ app, you have come to the right place
60 stars 37 forks source link

The whole data is not printing . #12

Closed RintuMondal06 closed 5 years ago

RintuMondal06 commented 5 years ago

Using this plugin i am able to print data dynamically . But the problem is, the whole invoice is not getting printed. I have checked the final output of the String in console, it is displaying properly there. But when i am sending the string to print, it's printing half of the Invoice. Please help me !!

Below is my Code : let printData = "\n\n KOLKATA BASKET \n\n\n"; printData = printData + "----------------------------" + "\n\n" + "Customer Name: " + details.name + "\n" + "Phone: " + details.mobile + "\n" + "Address:" + details.shipping_address + "\n"

razmans commented 5 years ago

You may need to use something called mutable buffer. This was my response to a similar issue last year. Hopefully this helps:

https://forum.ionicframework.com/t/printing-on-bluetooth-printers/17886/41

RintuMondal06 commented 5 years ago

Thanks sir @razmans !!! It's working fine now ! But having problem when i am using forEach loop , only the first iteration is printed and no code is excuted after the loop. products.forEach((element , index) => { this.buff.write(CMD.TEXT_FORMAT.TXT_ALIGN_LT); this.buff.write(CMD.TEXT_FORMAT.TXT_NORMAL); this.buff.write(CMD.TEXT_FORMAT.TXT_FONT_A); this.buff.write(Number(index+1) + ". "+element.product_name + " " + element.quantity + " Rs." + element.price ); this.buff.write(CMD.FEED_CONTROL_SEQUENCES.CTL_LF);

  });
blinkuz commented 5 years ago

Gracias señor @razmans !!! ¡Funciona bien ahora! Pero teniendo un problema cuando estoy usando forEach loop, solo se imprime la primera iteración y no se ejecuta ningún código después del loop. products.forEach ((element, index) => { this.buff.write (CMD.TEXT_FORMAT.TXT_ALIGN_LT); this.buff.write (CMD.TEXT_FORMAT.TXT_NORMAL); this.buff.write (CMD.TEXT_FORMAT.TXT_FONTAT.TXT_FONTAT. ; this.buff.write (Number (index + 1) + "." + element.product_name + "" + element.quantity + "Rs." + element.price); this.buff.write (CMD.FEED_CONTROL_SEQUENCES.CTL_LF );

  });

What version of mutableBuffer are you using? In my case I do the following:

 const buff = new MutableBuffer (1024, 1024);

And it allows me to add large amounts of elements in a loop and then continue with the lines outside the loop.

RintuMondal06 commented 5 years ago

Thank you @blinkuz for your kind response ! I'm using MutableBuffer Version ^2.0.3 . Here is my complete code ; testPrint(address , details , products:any = []) { let CMD = data; this.buff.clear();

this.buff.write(CMD.TEXT_FORMAT.TXT_ALIGN_CT);
this.buff.write(CMD.TEXT_FORMAT.TXT_BOLD_ON);
this.buff.write(CMD.TEXT_FORMAT.TXT_4SQUARE);
this.buff.write(CMD.TEXT_FORMAT.TXT_FONT_A);
this.buff.write(CMD.TEXT_FORMAT.TXT_BOLD_ON);
this.buff.write("Company Name");
this.buff.write(CMD.FEED_CONTROL_SEQUENCES.CTL_LF);
this.buff.write(CMD.TEXT_FORMAT.TXT_NORMAL);
this.buff.write(CMD.TEXT_FORMAT.TXT_BOLD_OFF);
this.buff.write(CMD.TEXT_FORMAT.TXT_FONT_A);
this.buff.write("Everything at your door");
this.buff.write(CMD.FEED_CONTROL_SEQUENCES.CTL_LF); //2 lines
this.buff.write(CMD.FEED_CONTROL_SEQUENCES.CTL_LF);
this.buff.write(CMD.FEED_CONTROL_SEQUENCES.CTL_LF);
this.buff.write(CMD.TEXT_FORMAT.TXT_ALIGN_LT);
this.buff.write(CMD.TEXT_FORMAT.TXT_FONT_A);
this.buff.write("Customer Name :" + details.name);
this.buff.write(CMD.FEED_CONTROL_SEQUENCES.CTL_LF);
this.buff.write("Phone  :" + details.mobile);
this.buff.write(CMD.FEED_CONTROL_SEQUENCES.CTL_LF);
this.buff.write("Address  :" + details.shipping_address + "," + details.shipping_pin + "," + 
details.shipping_city);
this.buff.write(CMD.FEED_CONTROL_SEQUENCES.CTL_LF);
this.buff.write(CMD.TEXT_FORMAT.TXT_ALIGN_CT);
this.buff.write(CMD.TEXT_FORMAT.TXT_BOLD_ON);
this.buff.write("----------------------");
this.buff.write(CMD.TEXT_FORMAT.TXT_BOLD_OFF);
this.buff.write(CMD.FEED_CONTROL_SEQUENCES.CTL_LF);
this.buff.write(CMD.TEXT_FORMAT.TXT_ALIGN_LT);
this.buff.write(CMD.TEXT_FORMAT.TXT_BOLD_OFF);
this.buff.write("OrderId  :" + details.unique_code);
this.buff.write(CMD.FEED_CONTROL_SEQUENCES.CTL_LF);
this.buff.write("Delivery Date :" + details.delivery_date);
this.buff.write(CMD.FEED_CONTROL_SEQUENCES.CTL_LF);
this.buff.write("Delivery Time :" + details.time_span);
this.buff.write(CMD.FEED_CONTROL_SEQUENCES.CTL_LF);
this.buff.write(CMD.TEXT_FORMAT.TXT_ALIGN_CT);
this.buff.write(CMD.TEXT_FORMAT.TXT_BOLD_ON);
this.buff.write("----------------------");
this.buff.write(CMD.TEXT_FORMAT.TXT_BOLD_OFF);
this.buff.write(CMD.FEED_CONTROL_SEQUENCES.CTL_LF);
this.buff.write(CMD.TEXT_FORMAT.TXT_ALIGN_LT);
this.buff.write("Sl. Name        Qty Price");
this.buff.write(CMD.FEED_CONTROL_SEQUENCES.CTL_LF);
this.buff.write(CMD.FEED_CONTROL_SEQUENCES.CTL_LF);

for (let index = 0; index < products.length; index++) {
  this.buff.write(CMD.TEXT_FORMAT.TXT_ALIGN_LT);
  this.buff.write(CMD.TEXT_FORMAT.TXT_NORMAL);
   this.buff.write(Number(index+1) + ".   "+products[index].product_name + "   " + products[index].quantity + " Rs." + products[index].price);
  this.buff.write(CMD.FEED_CONTROL_SEQUENCES.CTL_LF);
}

this.buff.write(CMD.FEED_CONTROL_SEQUENCES.CTL_LF);
this.buff.write(CMD.TEXT_FORMAT.TXT_BOLD_ON);
this.buff.write("Total Amount : Rs." + details.total_amount);
this.buff.write(CMD.TEXT_FORMAT.TXT_BOLD_OFF);
this.buff.write(CMD.FEED_CONTROL_SEQUENCES.CTL_LF);
this.buff.write(CMD.FEED_CONTROL_SEQUENCES.CTL_LF);
this.buff.write(CMD.FEED_CONTROL_SEQUENCES.CTL_LF);
console.log(this.buff);

let xyz=this.connectBT(address).subscribe(data=>{
  this.btSerial.clear();
  this.btSerial.write(this.buff.buffer).then(dataz=>{
  this.buff.clear();

    let mno=this.alertCtrl.create({
      title:"Print SUCCESS!",
      buttons:['Dismiss']
    });
    mno.present();

    xyz.unsubscribe();
  },errx=>{
    console.log("WRITE FAILED",errx);
    let mno=this.alertCtrl.create({
      title:"ERROR "+errx,
      buttons:['Dismiss']
    });
    mno.present();
  });
  },err=>{
    console.log("CONNECTION ERROR",err);
    let mno=this.alertCtrl.create({
      title:"ERROR "+err,
      buttons:['Dismiss']
    });
    mno.present();
  });

}