0

i am using InAppBrower to open pdf file it is not supporting the .pdf file so i choose to use gview for supporting it

My file path file:///data/user/0/com.schneider.and/files/Attendance_Policy_Ver_1.0.pdf

here is my code for inappbrower

//the below code works file
// var url = https://docs.google.com/gview?embedded=true&url=http://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf

//the below code is not working showing blank screen 
    var url = https://docs.google.com/gview?embedded=true&url=file:///data/user/0/com.schneider.and/files/Attendance_Policy_Ver_1.0.pdf ;


//my inappbrowser code
    const browser = this.iab.create(url, '_blank', 'location=yes
,EnableViewPortScale=yes');

i am getting blank screen in my inappbrowser window.

Mohan Gopi
  • 7,606
  • 17
  • 66
  • 117

1 Answers1

0

First of all refer this answer on launching pdf files in Inappbrowser in Android.

I think you cannot give your local file path in that way.

InAppBrowser is supported for web content mostly. its in a way similar to Androids WebView.

Quoting from Inappbrowser - Apache Cordova,

  • You can show helpful articles, videos, and web resources inside of your app. Users can view web pages without leaving your app.
  • The InAppBrowser window behaves like a standard web browser, and can't access Cordova APIs. For this reason, the InAppBrowser is recommended if you need to load third-party (untrusted) content

Do check this format for opening Local URIs (HTML pages).

You can use ngCordova-FileOpener2 for opening files (including pdf) from the device storage. This uses default application for opening particular type of file.

Working code snippet -

<body >
    <ion-pane>
      <ion-header-bar class="bar-stable">
        <h1 class="title">Ionic Blank Starter</h1>
      </ion-header-bar>
      <ion-content ng-controller="FileOpenerController">
        <button class="button button-full button-positive" ng-click="openPDF()">
          Open PDF File
        </button>
      </ion-content>
    </ion-pane>
</body>

Then in JavaScript file use this -

app.controller('FileOpenerController', function($scope, $cordovaFileOpener2, $ionicPlatform) {

    $scope.openPDF= function() {
        $cordovaFileOpener2.open(
            '/sdcard/Download/109.pdf',
            'application/pdf'
        ).then(function() {
            console.log('Success');
        }, function(err) {
        console.log('An error occurred: ' + JSON.stringify(err));
        });
    };           
});

Full tutorial along with the code can be found here.

pro_cheats
  • 1,534
  • 1
  • 15
  • 25
  • I tried your answer but i am not able to open the pdf file i am not even getting blank screen i am getting balck screen for 2 to 3 sec and then closing automatically – Mohan Gopi Jun 09 '17 at 07:18
  • Did you try the tutorial in this [link](http://www.gajotres.net/how-to-open-a-file-using-its-default-application-in-ionic-framework/2/) ?? (same as the one posted in answer) – pro_cheats Jun 09 '17 at 08:50