Open PDF file


#1

I have this code

downloadAndOpenPdf() {
    this.fileChooser.open()
      .then(uri => {
        this.filePath.resolveNativePath(uri)
          .then(filePath => {
            alert(uri);
            const options: DocumentViewerOptions = {
              title: 'My PDF'
            }
            this.document.viewDocument('assets/recibo.pdf', 'application/pdf', options, (show => alert("1: " + show)), (onClose => alert("2: " + onClose)), (onMissingApp => alert("3: " + onMissingApp)), (onError => alert("4: " + JSON.stringify(onError))))
          })
          .catch(err => console.log(err));
      }
      )
      .catch(e => console.log(e));
  }

when I ran the app, I got this error message

any idea?

thanks


#2

i think ionic have this plugin to oopen any kind of file


#3

First, sorry for my English, I’m learning

I tried with file opener 2, but I got two errors when I used

ionic cordova plugin add cordova-plugin-file-opener2
npm install --save @ionic-native/file-opener

1.- I can’t build the apk, so I removed the plugin and uninstalled file-opener.

* What went wrong:
Execution failed for task ':app:processDebugResources'.
> Failed to execute aapt

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 9s
    at ChildProcess.whenDone (D:\Ionic\last\platforms\android\cordova\node_modules\cordova-common\src\superspawn.js:169:23)
    at emitTwo (events.js:126:13)
    at ChildProcess.emit (events.js:214:7)
    at maybeClose (internal/child_process.js:925:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
(node:15476) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by
rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:15476) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

[OK] Your app has been deployed.
     Did you know you can live-reload changes from your app with --livereload?

2 after that, I install the plugin and file-opener but the alert message was: Error opening file: plugin_not_installed

this is my code

  openLocalPdf() {

    this.fileOpener.open('/assets/file.pdf', 'application/pdf')
      .then(() => alert('File is opened'))
      .catch(e => alert('Error opening file: ' + e));
  }

the plugin isn’t in the package.json

"dependencies": {
    "@angular/animations": "5.2.11",
    "@angular/common": "5.2.11",
    "@angular/compiler": "5.2.11",
    "@angular/compiler-cli": "5.2.11",
    "@angular/core": "5.2.11",
    "@angular/forms": "5.2.11",
    "@angular/http": "5.2.11",
    "@angular/platform-browser": "5.2.11",
    "@angular/platform-browser-dynamic": "5.2.11",
    "@ionic-native/core": "4.8.0",
    "@ionic-native/document-viewer": "^4.9.1",
    "@ionic-native/file": "^4.9.1",
    "@ionic-native/file-chooser": "^4.9.1",
    "@ionic-native/file-opener": "^4.9.1",//<-----
    "@ionic-native/file-path": "^4.9.1",
    "@ionic-native/file-transfer": "^4.9.1",
    "@ionic-native/in-app-browser": "^4.9.1",
    "@ionic-native/splash-screen": "4.8.0",
    "@ionic-native/status-bar": "4.8.0",
    "@ionic/storage": "2.1.3",
    "cordova-android": "7.0.0",
    "cordova-plugin-device": "^2.0.2",
    "cordova-plugin-document-viewer": "^0.9.10",
    "cordova-plugin-file": "^6.0.1",
    "cordova-plugin-file-opener2": "^2.0.19",//<----
    "cordova-plugin-file-transfer": "^1.7.1",
    "cordova-plugin-filechooser": "^1.0.1",
    "cordova-plugin-filepath": "^1.3.0",
    "cordova-plugin-inappbrowser": "^3.0.0",
    "cordova-plugin-ionic-keyboard": "^2.0.5",
    "cordova-plugin-ionic-webview": "^1.1.19",
    "cordova-plugin-splashscreen": "^5.0.2",
    "cordova-plugin-whitelist": "^1.3.3",
    "ionic-angular": "3.9.2",
    "ionicons": "3.0.0",
    "rxjs": "5.5.11",
    "sw-toolbox": "3.6.0",
    "zone.js": "0.8.26"
  },
"plugins": {
      "cordova-plugin-file": {},
      "cordova-plugin-file-transfer": {},
      "cordova-plugin-document-viewer": {},
      "cordova-plugin-whitelist": {},
      "cordova-plugin-device": {},
      "cordova-plugin-splashscreen": {},
      "cordova-plugin-ionic-webview": {},
      "cordova-plugin-ionic-keyboard": {},
      "cordova-plugin-filechooser": {},
      "cordova-plugin-filepath": {},
      "cordova-plugin-inappbrowser": {}
    },

if I add the plugin in the plugins
“cordova-plugin-file-opener2”: {},

I get the same error

if i do

ionic cordova plugin add cordova-plugin-file-opener2

the message is

Error during processing of action! Attempting to revert...

Failed to install 'cordova-plugin-file-opener2': CordovaError: Uh oh!
"D:\Ionic\ManualsBrowser\platforms\android\app\src\main\res\xml\opener_paths.xml" already exists!
    at copyNewFile (D:\Ionic\ManualsBrowser\platforms\android\cordova\lib\pluginHandlers.js:261:45)
    at install (D:\Ionic\ManualsBrowser\platforms\android\cordova\lib\pluginHandlers.js:51:17)
    at ActionStack.process (D:\Ionic\ManualsBrowser\platforms\android\cordova\node_modules\cordova-common\src\ActionStack.js:56:25)
    at PluginManager.doOperation (D:\Ionic\ManualsBrowser\platforms\android\cordova\node_modules\cordova-common\src\PluginManager.js:114:20)
    at PluginManager.addPlugin (D:\Ionic\ManualsBrowser\platforms\android\cordova\node_modules\cordova-common\src\PluginManager.js:144:17)
    at D:\Ionic\ManualsBrowser\platforms\android\cordova\Api.js:247:74
    at _fulfilled (D:\Ionic\ManualsBrowser\platforms\android\cordova\node_modules\q\q.js:854:54)
    at self.promiseDispatch.done (D:\Ionic\ManualsBrowser\platforms\android\cordova\node_modules\q\q.js:883:30)
    at Promise.promise.promiseDispatch (D:\Ionic\ManualsBrowser\platforms\android\cordova\node_modules\q\q.js:816:13)
    at D:\Ionic\ManualsBrowser\platforms\android\cordova\node_modules\q\q.js:570:49

(node:1636) UnhandledPromiseRejectionWarning: CordovaError: Uh oh!
"D:\Ionic\ManualsBrowser\platforms\android\app\src\main\res\xml\opener_paths.xml" already exists!
    at copyNewFile (D:\Ionic\ManualsBrowser\platforms\android\cordova\lib\pluginHandlers.js:261:45)
    at install (D:\Ionic\ManualsBrowser\platforms\android\cordova\lib\pluginHandlers.js:51:17)
    at ActionStack.process (D:\Ionic\ManualsBrowser\platforms\android\cordova\node_modules\cordova-common\src\ActionStack.js:56:25)
    at PluginManager.doOperation (D:\Ionic\ManualsBrowser\platforms\android\cordova\node_modules\cordova-common\src\PluginManager.js:114:20)
    at PluginManager.addPlugin (D:\Ionic\ManualsBrowser\platforms\android\cordova\node_modules\cordova-common\src\PluginManager.js:144:17)
    at D:\Ionic\ManualsBrowser\platforms\android\cordova\Api.js:247:74
    at _fulfilled (D:\Ionic\ManualsBrowser\platforms\android\cordova\node_modules\q\q.js:854:54)
    at self.promiseDispatch.done (D:\Ionic\ManualsBrowser\platforms\android\cordova\node_modules\q\q.js:883:30)
    at Promise.promise.promiseDispatch (D:\Ionic\ManualsBrowser\platforms\android\cordova\node_modules\q\q.js:816:13)
    at D:\Ionic\ManualsBrowser\platforms\android\cordova\node_modules\q\q.js:570:49
(node:1636) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1636) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.