Search code examples
javascriptangularionic-frameworkcharacter-encodingescpos

Encode string to cp852 in Angular/Ionic - iconv-lite


iam working on mobile App, which uses bluetooth printer to print bills. To get bill printed with correct diacritics, i need to encode it to correct coddepage. This can be done with this iconv-lite library, but iam not able to get it work. Has anyone experience with iconv-lite usage with ionic? Or is there any other way to encode strings in ionic?

I succesfully installed it, imported it in my printerService and try to run ionic serve, i get the following error, when i try to run testEncoding method, which uses iconvlite:

core.js:6014 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'isEncoding' of undefined
TypeError: Cannot read property 'isEncoding' of undefined
    at Object../node_modules/string_decoder/lib/string_decoder.js (string_decoder.js:29)
    at __webpack_require__ (bootstrap:84)
    at Object../node_modules/iconv-lite/encodings/internal.js (internal.js:49)
    at __webpack_require__ (bootstrap:84)
    at Object../node_modules/iconv-lite/encodings/index.js (index.js:6)
    at __webpack_require__ (bootstrap:84)
    at Object.getCodec (index.js:65)
    at Object.getEncoder (index.js:117)
    at Object.encode (index.js:22)
    at PrintService.testEncode (print.service.ts:26)
    at resolvePromise (zone-evergreen.js:797)
    at zone-evergreen.js:862
    at ZoneDelegate.invokeTask (zone-evergreen.js:391)
    at Object.onInvokeTask (core.js:39680)
    at ZoneDelegate.invokeTask (zone-evergreen.js:390)
    at Zone.runTask (zone-evergreen.js:168)
    at drainMicroTaskQueue (zone-evergreen.js:559)
    at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:469)
    at invokeTask (zone-evergreen.js:1603)
    at HTMLElement.globalZoneAwareCallback (zone-evergreen.js:1629)

code in my printservice looks like this

import { Injectable } from '@angular/core';
import { BluetoothSerial } from '@ionic-native/bluetooth-serial/ngx';
import { Storage } from '@ionic/storage';
import { Platform } from '@ionic/angular';
import { switchMap, tap, catchError } from 'rxjs/operators';
import { from, throwError } from 'rxjs';
import * as iconvlite from 'iconv-lite';


    @Injectable({
      providedIn: 'root'
    })
    export class PrintService {
    ...

      testEncoding(){
        console.log(iconvlite.encode("testžýáč", "cp852")); 
      }

Weird thing is, that even when i change testEncoding method to

console.log(iconvlite.encodingExists("cp852"));

It returns encoding not recognized error, despite the name of encoding is correct.

I get same errors even when i tried to use this ESC POS ionic library - https://github.com/Ans0n-Ti0/EscPosEncoder(with difference, that the error when i run ionic serve says “Unknown Codepage” even if iam entering correct codepage like cp852)

Any ideas where is the problem? iam using ionic 5 and angular 8. Or are there any other ways to convert utf8 string to cp852 in javascript? Iam trying to solve this issule for a long time, but i cant find solution. Maybe i need to install some additional packages to run iconvlite on frontend?


Solution

  • It seems there was some problem with angular 8 , probably with its webpack setup, when i upgraded to newer version, it works smoothly.