auctifera-josed / starprnt

Cordova plugin for the Star micronics printers
MIT License
40 stars 39 forks source link

printImage method not working with base64 images #39

Closed geraldosalazar16 closed 5 years ago

geraldosalazar16 commented 5 years ago

Hi I have been trying to print a base64 encoded image, from an Ionic 4 app. When I use the file path of the image everything works fine, but trying the base64 encoded image just close my app without any error message.

A small code snippet to show my point

const imageObj: ImageObj = { uri: base64EncodedImage, paperWidth: 576, // options: 384 = 2", 576 = 3", 832 = 4" cutReceipt: true, // Defaults to true openCashDrawer: true // Defaults to true }; const resultPrintImage = await this.starPrinterService.printImage(printer.data.portName,printer.data.emulation, imageObj);

infoxicator commented 5 years ago

This is by design, base64 images have to be loaded to the phone’s memory and create performance issues. You should get the image from the file system.

geraldosalazar16 commented 5 years ago

Well I really needed to print the base64 image because my image come from the database in that format. I figured out how to do it. The problem is the Ionic Native package. It turns out it only has the printImage function and not the printBase64Image from the cordova plugin. I was trying to use printImage and passing the base64 encoded string in the uri param, and that was wrong. So all I did was use the cordova plugin instead ok the native ionic wrapper. declare var starprnt; async printBase64Image(port: string, emulation: string, imageObj: any) { starprnt.printBase64Image(port, emulation, imageObj, result => { return result; }, error => { return error; }); }

Also, the javascript object to pass to the printBase64Image is different from the one you have to use with printImage, as I notice latter in the docs For base64 var imageObj = { base64Image: 'put base64 image string here', width: 576 // options: 384 = 2", 576 = 3", 832 = 4" cutReceipt:true, // Defaults to true openCashDrawer:true // Defaults to true }; For printImage var imageObj = { uri: 'file:///...', width: 576 // options: 384 = 2", 576 = 3", 832 = 4" cutReceipt:true, // Defaults to true openCashDrawer:true // Defaults to true }; Hope it helps somebody :)