I'm trying to open a PDF file in my app which I'm building with Ionic. I understand that I can do that using the inAppBrowser.
I first get the PDF from an api endpoint, after which I write it to the filesystem. Once it is written to the file system, I want to open it using the inAppBrowser. The code I have now is as follows:
$http.get(window.apiUrl + '/doc/54bfcc7b879c686969d2bbfe')
.success(function(data){
console.log('PDF GOTTEN SUCCESFULLY. NOW WRITE IT TO A FILE.');
$cordovaFile.writeFile('ticket.pdf', data, true)
.then(function (success) {
console.log('PDF WAS WRITTEN TO THE FILE SYSTEM');
console.log(JSON.stringify(success));
$cordovaFile.readAsText('ticket.pdf')
.then(function (data) {
console.log('PDF COULD BE READ FROM THE SYSTEM');
window.open('cdvfile://localhost/persistent/ticket.pdf', '_system', 'location=no');
console.log('AFTER OPENING THE PDF');
}, function (error) {
console.log(JSON.stringify(error));
});
}, function (error) {
console.log(JSON.stringify(error));
});
})
.error(function(error){
console.log('IMAGE GET FAILURE.');
});
I get no errors whatsoever in the console, but nothing happens in the screen. The output I see in the console.log is as follows:
file:///android_asset/www/js/controllers.js: Line 104 : PDF GOTTEN SUCCESFULLY. NOW WRITE IT TO A FILE.
file:///android_asset/www/js/controllers.js: Line 110 : PDF WAS WRITTEN TO THE FILE SYSTEM
file:///android_asset/www/js/controllers.js: Line 111 : {"type":"writeend","bubbles":false,"cancelBubble":false,"cancelable":false,"lengthComputable":false,"loaded":0,"total":0,"target":{"fileName":"","length":598874,"localURL":"cdvfile://localhost/persistent/ticket.pdf","position":598874,"readyState":2,"result":null,"error":null,"onwritestart":null,"onprogress":null,"onwrite":null,"onabort":null,"onerror":null}}
file:///android_asset/www/js/controllers.js: Line 115 : PDF COULD BE READ FROM THE SYSTEM
file:///android_asset/www/js/controllers.js: Line 119 : AFTER OPENING THE PDF
As you can see I hardcoded the ticket name (ticket.pdf
) and the path when opening it up (cdvfile://localhost/persistent/ticket.pdf
). The file path I got from the json success-output as displayed in the output on line 111 (see above).
Does anbody know why this isn't working? All tips are welcome!
The url cdvfile://localhost/persistent/ticket.pdf is not the correct one. You will need a nativeurl path starting with file://.
try these:
'file:///storage/emulated/0/ticket.pdf' or 'file:///storage/sdcard0/ticket.pdf'