1

I have been trying to open PDF files in a Ionic app. I have tried many Cordova FileOpener2 plugin and in app browser, none of them worked.

Those are the functions I tried , when I test it in the browser it gives me error " cordova is not defiened , and when I test it on the emulator or on the phone , the functions doesn't work:

$scope.openPDFI = function(){

    cordova.plugins.fileOpener2.open(
        'cdvfile://localhost/persistent/pdfs/BT168562.3.pdf', // You can also use a Cordova-style file uri: cdvfile://localhost/persistent/Download/starwars.pdf
    'application/pdf', 
    { 
        error : function(e) { 
            console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
        },
        success : function () {
            console.log('file opened successfully');                
        }
    }
)
};
**/
 /**$scope.openPDFI = function() {

   window.resolveLocalFileSystemURL(cordova.file.applicationDirectory +  'www/pdfs/BT168562.3.pdf', function(fileEntry) {

    window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dirEntry) {

        fileEntry.copyTo(dirEntry, uri.split('/').pop(), function(newFileEntry) {

            window.open(newFileEntry.nativeURL, '_system');
        });
    });
});
 };
 **/



  //  $scope.openPDF = function() {
  //window.open('www/pdfs/BT168562.3.pdf', '_system', 'location=yes');
//};


       /**
$scope.openPDFf = function(filename) {

newfilename = 'file.pdf'
uri = '/www/pdfs/' + filename;
window.resolveLocalFileSystemURL(cordova.file.applicationDirectory + uri, function(fileEntry) {

  window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dirEntry) {

    fileEntry.copyTo(dirEntry, newfilename, function(newFileEntry) {

      window.open(newFileEntry.nativeURL, '_system', 'location=yes');
    });
  });
});
};
**/
 /**$scope.openPDF = function(){
window.resolveLocalFileSystemURL(cordova.file.applicationDirectory +  'www/pdfs/BT168562.3.pdf', function(fileEntry) {
    window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dirEntry) {
        fileEntry.copyTo(dirEntry, 'file.pdf', function(newFileEntry) {
            cordova.plugins.fileOpener2.open(newFileEntry.nativeURL,'application/pdf',
            { 
                error : function(e) { 
                    console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
                },
                success : function () {
                    console.log('file opened successfully');                
                }
            }
            );
        });
    });
});
};
**/
 /**$scope.downloadFile= function() {
        $cordovaFileOpener2.open(
            '/sdcard/pdfs/BT168562.3.pdf',
            'application/pdf'
        ).then(function() {
            console.log('Success');
        }, function(err) {
        console.log('An error occurred: ' + JSON.stringify(err));
        });
    };    **/       


/**
$scope.onFileOpen =function() {
        cordova.plugins.fileOpener2.open(
            cordova.file.applicationDirectory + '/www/pdfs/BT168562.3.pdf',
            'application/pdf',
            {
                error: function() {

                },
                success: function() {

                }
            }
        );
    }


$scope.loadPDF =function () {
    console.log('loadPDF1');
    document.location.href='pdfs/BT168562.3.pdf';
}

$scope.openPDF =  function () {
    console.log('openPdf');
    var ref = cordova.InAppBrowser.open('pdfs/BT168562.3.pdf', '_blank', 'location=no');
}

/** $scope.loadPDF =function () {
    console.log('loadPDF3');
    console.log(cordova.file.applicationDirectory);
    cordova.plugins.fileOpener2.open(
        cordova.file.applicationDirectory+'www/pdfs/BT168562.3.pdf',
        'application/pdf', 
        { 
            error : function(e) { 
                console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
            },
            success : function () {
                console.log('file opened successfully');                
            }
        }
    );
} 

<button ng-click="onFileOpen()">Open pdf</button> </br>
                <button ng-click="loadPDF()">load pdf</button>

Any idea on how can I do this?

Thanks a lot.

Mirza Sisic
  • 2,401
  • 4
  • 24
  • 38
Amr Ayoub
  • 73
  • 1
  • 9
  • Cordova is not loaded in the ionic when you try to debug in a web browser itself. Read this to debug. http://stackoverflow.com/questions/21332853/is-there-a-real-solution-to-debug-cordova-apps – Dinesh Devkota Sep 10 '16 at 00:57
  • yes i know , but also it's not working on the device or emulator . i need a function that work , i tried those above and they didn't work – Amr Ayoub Sep 10 '16 at 12:16
  • @Amr Ayoub try out this working sample in device - https://github.com/gandhirajan/Cordova_File_Operations – Gandhi Sep 11 '16 at 04:11
  • @Gandhi it looks good , but this phonegap code , how can i implement this on ionic application – Amr Ayoub Sep 11 '16 at 13:36
  • @Amr Ayoub you jus have to use ionic equivalent plugins of the same. The code remains the same. Only change may be to use plugin code in platform ready function instead of device ready – Gandhi Sep 11 '16 at 14:01
  • @Gandhi Thank you , i will try and let you know how did it go :) – Amr Ayoub Sep 11 '16 at 17:36
  • @Gandhi I spent hours trying it but didn't work , i test it on the phone and nothing show up , i guess it an issue with the file path , any help please – Amr Ayoub Sep 11 '16 at 21:57
  • @AmrAyoub Unfortunately i dont have ionic setup for me to test. But you could send me the apk for testing – Gandhi Sep 12 '16 at 05:46

0 Answers0