0
votes

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.

1

1 Answers

1
votes

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.