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.