thiendangit / react-native-thermal-receipt-printer-image-qr

React native thermal receipt printer
https://www.npmjs.com/package/react-native-thermal-receipt-printer-image-qr
111 stars 73 forks source link

printImageBase64 in iOS #87

Closed chukiatt closed 1 year ago

chukiatt commented 1 year ago

hi thiendangit

i am chukiat from thailand it very long time to talk with you, i hope you are very well.

to day i have some question about print receipt i need to draw receipt in canvas and export to dataURL base64 (for support thai CharacterData reason) i use printImageBase64 in android it can print right but in iOS it does not print anything. i try many imageWidth and height but also still does not work

this.canvas.toDataURL() .then((data) => { const _data = data.replace(/(\r\n|\n|\r)/gm, "").replace('"', '').replace('data:image/png;base64,', '').replace(/^.+,/, ''); console.log(_data);

// print from printImageBase64 it does not work this.printer.printImageBase64(_data,{imageWidth: 125, imageHeight: 300, paddingX:0} )

// but save base64 to image and print from printImage() it work // this.printer.printImage('https://i.ibb.co/VJM71M6/cbimage.png', {imageWidth: 125, imageHeight: 300, paddingX:0} )

}) .catch((error) => console.log(error)); }


my package.json "react": "18.1.0", "react-native": "0.70.5", "react-native-webview": "^11.23.1", "react-native-canvas": "^0.1.38", "react-native-ping": "^1.2.7", "react-native-thermal-receipt-printer-image-qr": "^0.1.10",


thank you very much

chukiatt commented 1 year ago

i track you source RNBLEPrinter.m variable url has a value but imageData is return nil

RCT_EXPORT_METHOD(printImageBase64:(NSString )base64Qr printerOptions:(NSDictionary )options fail:(RCTResponseSenderBlock)errorCallback) { @try {

    !m_printer ? [NSException raise:@"Invalid connection" format:@"Can't connect to printer"] : nil;
    if(![base64Qr  isEqual: @""]){
        NSString *result = [@"data:image/png;base64," stringByAppendingString:base64Qr];      

        /////////////////////////////
        // url has return a value                  
        NSURL *url = [NSURL URLWithString:result];

        // but imageData return nil
        NSData *imageData = [NSData dataWithContentsOfURL:url];
        /////////////////////////////

        NSString* printerWidthType = [options valueForKey:@"printerWidthType"]; 
        NSInteger printerWidth = 576;

        if(printerWidthType != nil && [printerWidthType isEqualToString:@"58"]) {
            printerWidth = 384;
        }

        if(imageData != nil){
            UIImage* image = [UIImage imageWithData:imageData];
            UIImage* printImage = [self getPrintImage:image printerOptions:options];

            [[PrinterSDK defaultPrinterSDK] setPrintWidth:printerWidth];
            [[PrinterSDK defaultPrinterSDK] printImage:printImage ];
        }
    }
} @catch (NSException *exception) {
    errorCallback(@[exception.reason]);
}

}

chukiatt commented 1 year ago

ok problem solved. i have wrong image data.

thank you