paystory-de / thermal-printer-cordova-plugin

Cordova wrapper for ESC/POS Thermal Printer library
Apache License 2.0
50 stars 39 forks source link

image printing issue #15

Closed rizwanbao closed 2 years ago

rizwanbao commented 2 years ago

hi.. [C]" + PrinterTextParserImg.bitmapToHexadecimalString(printer, this.getApplicationContext().getResources().getDrawableForDensity(R.drawable.logo, DisplayMetrics.DENSITY_MEDIUM))+"\n" ReferenceError: PrinterTextParserImg is not defined this above code not working not getting or picking image and print can you please share some example of how to printer read the images

patrickbussmann commented 2 years ago

This is a example of my code.

async urlToBase64(printer, url: string): Promise<string> {
  const imageFile = await lastValueFrom(
    this.http.get(url, { responseType: 'blob' })
  );
  const base64: string = await new Promise((resolve) => {
    const reader = new FileReader();
    reader.onloadend = () => {
      resolve(reader.result as string);
    };
    reader.readAsDataURL(imageFile);
  });
  return await new Promise((resolve, reject) =>
    ThermalPrinter.bitmapToHexadecimalString(
      {
        type: printer.connectionType,
        id: printer.productName || printer.name,
        base64
      },
      resolve,
      reject
    )
  );
}

Its from angular. So in theory you can understand how it works.

Reading the file you want to print as base64 and then convert it to hex-string.

Then you can embed it in the string like:

[C]" + PrinterTextParserImg.bitmapToHexadecimalString(printer, this.getApplicationContext().getResources().getDrawableForDensity(R.drawable.logo, DisplayMetrics.DENSITY_MEDIUM))+"\n"

==

"[C]" + (await this.urlToBase64(printer, 'URL-to-Image')) + "\n"
rizwanbao commented 2 years ago

thats great thanks