Open pdf angularjs from Array Buffer REST API using cordova ionic

Posted: March 12, 2015 in web

REST api return a pdf type Array Buffer

angular.module('pdf', [])
        .factory('PdfFactory', ['$http','$cordovaFileTransfer', '$cordovaFileOpener2',
            function ($http, $cordovaFileTransfer, $cordovaFileOpener2) {
                var pdfService = {
                    getPDF: function (data) {
                        var url = 'some rest url';
                        var targetPath = (ionic.Platform.isIOS() ? cordova.file.documentsDirectory : cordova.file.externalDataDirectory) + "sample.pdf";
                        var trustHosts = true;
                        var options = {headers: {"APIKey": 'somekey'}};
                        $cordovaFileTransfer.download(url, targetPath, options, trustHosts)
                                .then(function (result) {
                                    $cordovaFileOpener2.open(
                                            targetPath,
                                            'application/pdf'
                                            ).then(function () {
                                    }, function (err) {
					//error
                                    });

                                }, function (err) {
                                    // TODO
                                });
                    }
                };
                return pdfService;
            }]);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s