A mysterious issue with Cordova Printer Plugin (Android)

Hi everyone,

I have cordova printer plugin added to my project, the plugin is imported in my project app.module.ts (included in providers).

The plugin works well with iOS, put with android, I don’t get any error via the console, the checking for printing support is done with no errors, I even see my pre-set console messages, but nothing happens on the phone when I press the print button. The file is pdf and it’s generated by pdfmake and already saved in Documents folder on android device… the file is there and has no problems.

Console logs from android studio:

V/Capacitor/Plugin: To native (Cordova plugin): callbackId: File1163166029, service: File, action: resolveLocalFileSystemURI, actionArgs: ["file:\/\/\/storage\/emulated\/0\/Documents\/Doc-1582115145298.pdf"]
V/Capacitor/Plugin: To native (Cordova plugin): callbackId: Printer1163166030, service: Printer, action: check, actionArgs: [null]
I/Capacitor/Console: File: http://localhost/myapp-module-es2015.js - Line 70632 - Msg: printing available
I/Capacitor/Console: File: http://localhost/myapp-module-es2015.js - Line 70634 - Msg: trying to print...
V/Capacitor/Plugin: To native (Cordova plugin): callbackId: Printer1163166031, service: Printer, action: print, actionArgs: ["file:\/\/\/storage\/emulated\/0\/Documents\/Doc-1582115145298.pdf",{"name":"Doc-1582115145298.pdf","orientation":"portrait"}]

Plugin details:

"@ionic-native/printer": "^5.21.5",
"cordova-plugin-printer": "^0.8.0",

My package.json content:

{
  "name": "myapp",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "https://ionicframework.com/",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "~8.1.2",
    "@angular/core": "~8.1.2",
    "@angular/fire": "^5.4.2",
    "@angular/forms": "~8.1.2",
    "@angular/platform-browser": "~8.1.2",
    "@angular/platform-browser-dynamic": "~8.1.2",
    "@angular/router": "~8.1.2",
    "@angular/service-worker": "^8.2.14",
    "@capacitor/android": "^1.5.0",
    "@capacitor/core": "1.4.0",
    "@capacitor/ios": "^1.5.0",
    "@ionic-native/background-mode": "^5.21.5",
    "@ionic-native/camera": "^5.21.5",
    "@ionic-native/core": "^5.21.5",
    "@ionic-native/file": "^5.21.5",
    "@ionic-native/file-opener": "^5.21.5",
    "@ionic-native/printer": "^5.21.5",
    "@ionic-native/social-sharing": "^5.21.5",
    "@ionic/angular": "^4.7.1",
    "@ionic/pwa-elements": "^1.4.2",
    "cordova-plugin-background-mode": "^0.7.3",
    "cordova-plugin-camera": "^4.1.0",
    "cordova-plugin-device": "^2.0.3",
    "cordova-plugin-file": "^6.0.2",
    "cordova-plugin-file-opener2": "^3.0.0",
    "cordova-plugin-printer": "^0.8.0",
    "cordova-plugin-x-socialsharing": "^5.6.4",
    "core-js": "^3.6.4",
    "es6-promise-plugin": "^4.2.2",
    "firebase": "^7.8.1",
    "moment": "^2.24.0",
    "pdfmake": "^0.1.64",
    "rxjs": "~6.5.1",
    "tslib": "^1.9.0",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/architect": "~0.801.2",
    "@angular-devkit/build-angular": "^0.803.25",
    "@angular-devkit/core": "~8.1.2",
    "@angular-devkit/schematics": "~8.1.2",
    "@angular/cli": "~8.1.2",
    "@angular/compiler": "~8.1.2",
    "@angular/compiler-cli": "~8.1.2",
    "@angular/language-service": "~8.1.2",
    "@capacitor/cli": "^1.5.0",
    "@ionic/angular-toolkit": "^2.1.1",
    "@ionic/lab": "3.0.1",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "^5.0.0",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.5.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.15.0",
    "typescript": "~3.4.3"
  },
  "description": "My App",
  "cordova": {
    "plugins": {
      "cordova-plugin-camera": {},
      "cordova-plugin-background-mode": {},
      "cordova-plugin-file": {},
      "cordova-plugin-file-opener2": {},
      "cordova-plugin-printer": {},
      "cordova-plugin-x-socialsharing": {}
    }
  }
}

Please see my code below:

async printPdf() {
    const exists = await this.checkFile();
    if (exists) {
      this.printer.check().then(
        () => {
          console.log('printing available');
          this.printer.pick().then(
            () => {
              console.log('trying to print...');
              this.printer.print(this.orgUri, {name: `Doc-${this.reportId}.pdf`, orientation: 'portrait'}).catch(
                () => {
                  this.utilServ.toastUtil({
                    message: 'Failed to print PDF file. Please try again',
                    color: 'dark',
                    duration: '3500',
                    position: 'top',
                    animated: 'true'
                  });
                });
            }
          );
        }, async (err) => {
          const alrt = await this.alrtCtrl.create({
            header: 'Printing Error',
            message: `Printing Not Supported On This Device`,
            cssClass: 'alert-box',
            buttons: [
              {
                text: 'Okay',
                role: 'Cancel'
              }
            ]
          });
          await alrt.present();
        }
      );
    } else {
      this.noFileAlert();
    }
  }

I also did try using isAvailable() method instead of check(), using print() without pick(), but it always gives same result as seen above on android logs console, and with every method mentioned, iOS works fine and print successfully.

My ‘ionic info’ command result:

Ionic:

   Ionic CLI                     : 5.4.14 (/usr/local/lib/node_modules/ionic)
   Ionic Framework               : @ionic/angular 4.11.10
   @angular-devkit/build-angular : 0.803.25
   @angular-devkit/schematics    : 8.1.3
   @angular/cli                  : 8.1.3
   @ionic/angular-toolkit        : 2.1.2

Capacitor:

   Capacitor CLI   : 1.5.0
   @capacitor/core : 1.4.0

Cordova:

   Cordova CLI       : 9.0.0 (cordova-lib@9.0.1)
   Cordova Platforms : none
   Cordova Plugins   : no whitelisted plugins (5 plugins total)

Utility:

   cordova-res : not installed
   native-run  : not installed

System:

   NodeJS : v12.14.1 (/usr/local/bin/node)
   npm    : 6.13.7
   OS     : macOS Catalina

Android details (real device):

OS: Android 9.0 (Havoc OS 2.9)
Xiaomi: Mi 5

By the way, printing from other apps like Drive PDF Viewer works on my phone, so I can safely say it’s not a printing service problem.

I did google every possible keyword that came to mind but no luck yet, so I really hope anyone can shed some light on what the problem is and how it can be fixed… Thanks a lot.

1 Like

Hi, I have the same issue with Android. Any clue?

Regards,

Samuel

Hi, I have the same issue with Android