0

Currently i am working on opening pdf document in angularjs w.r.t. desktop/ mobile also. i have followed this ref.: Open a PDF in a new window of the browser with angularjs there similar implementation i have done : code is as follows

**$http.get('generatePdfUrl')
  .then(function (data) {     // data is your url
      var file = new Blob([data], {type: 'application/pdf'});
      var fileURL = URL.createObjectURL(file);
  });**

even i have applied responsetype : 'arraybuffer' as well, in the angularjs web application this url points to blob://http://localhost:8080/126gyesdfsdfadjf, its is opening pdf document properly with window.open(fileURL), but the same is not working for the angularjs mobile application using cordova build, there application url points out to blob:file///126gyesdfsdfadjf, but unable to open pdf document, can anybody have suggestions on this. regars, vasu.

vasu
  • 43
  • 1
  • 8

1 Answers1

1

For this, you need to install a plugin for opening the pdf document.

function writePDFToFile(fileName, data){
        try{
            window.resolveLocalFileSystemURL(cordova.file.externalApplicationStorageDirectory, function(directoryEntry){
                directoryEntry.getFile(fileName, { create: true }, function(fileEntry){
                    fileEntry.createWriter(function(fileWriter){
                        fileWriter.onwriteend = function(e){
                            cordova.plugins.fileOpener2.open(cordova.file.externalApplicationStorageDirectory + fileName, 'application/pdf',{
                                error: function(e){
                                    console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
                                },
                                success: function () {
                                    console.log('file opened successfully');
                                }
                            });
                        };

                        fileWriter.onerror = function(e){
                            alert(e);
                        };

                        var blob = new Blob([data], { type: 'application/pdf' });
                        fileWriter.write(blob);

                    }, function onerror(e){
                        alert(e);
                    });
                }, function onerror(e){

                    alert(e);
                });
            },function onerror(e){            
                alert(e);
            });
        }catch(e){
            alert(e);
        }
    }

above implementation worked for me. Check this link for plugin.

scary_devil
  • 268
  • 4
  • 19
  • but in my case, i have url : blob:file://fcxysdjfsjdkfj, this is not working for me, & so it returns filenot found – vasu Jun 13 '17 at 12:59
  • Hi all cordova pdf document opening wroks fine w.r.t. ios devices & i am facing issues with android devices, plz let me know if any suggestions, as per my requirement i need to open pdf document with any existing third party apps. – vasu Jun 18 '17 at 08:43
  • Please check my updated answer. I'm getting json response as ArrayBuffer and I'm passing the same in th writePDFToFile function as 'data' argument. This is a working code in android. – scary_devil Jun 19 '17 at 06:19
  • thanks for info. this is working superb in android devices. – vasu Jun 19 '17 at 10:00