Generating PDF files in Ionic from a pdfstring already generated on the client side and store it in the device

I am working on Ionic hydrid app. I want to generate a pdf file from already generated pdfstring on the client side and store it in the device locally.

Generated the pdfstring using jspdf doc.output("datauristring");

the pdfstring looks something like this:

data:application/pdf;base64,JVBERi0xLjMKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMSAwIFIKL1Jlc291cmNlcyAyIDAgUgovQ29udGVudHMgNCAwIFI+PgplbmRvYmoKNCAwIG9iago8PC9MZW5ndGggMTA3Nz4+CnN0cmVhbQowLjU3IHcKMCBHCkJUCi9GMSAxMCBUZgoxMCBUTAowIGcKMjEyLjYwIDgxMy41NCBUZAooT3JkZXIgTnVtYmVyIDogMS0zMjAzNCkgVGoKRVQKQlQKL0YxIDEwIFRmCjEwIFRMCjAgZwo1Ni42OSA3ODUuMjAgVGQKKE9yZGVyIFR5cGUgOiBTZXJ2aWNlIE9yZGVyKSBUagpFVApCVAovRjEgMTAgVGYKMTAgVEwKMCBnCjU2LjY5IDc1Ni44NSBUZAooT3JkZXIgU3RhdHVzIDogUGVuZGluZykg ....

I would like to store the above content into a pdf file and store it in a location from which the application can access whenever required and display it to the user.

tried the following but doesnt seem to work

$cordovaFile.createFile(fileDir + 'test/one/Invoice.pdf',true)
.then(function(fileEntry) {
                        });

$cordovaFile.writeFile( fileDir + 'test/one/Invoice.pdf',$scope.pdfString, '')
.then(function(result) {
});

Upvotes: 0

Views: 296

Answers (1)

Jess Patton
Jess Patton

Reputation: 2486

Add this factory:

 angular.module('ionic.utils', [])
.factory('$localstorage', ['$window', function($window) {
  return {
    set: function(key, value) {
      $window.localStorage[key] = value;
    },
    get: function(key, defaultValue) {
      return $window.localStorage[key] || defaultValue;
    },
    setObject: function(key, value) {
      $window.localStorage[key] = JSON.stringify(value);
    },
    getObject: function(key) {
      return JSON.parse($window.localStorage[key] || '{}');
    }
  }
}]);

Then you can put it in a controller or run function:

angular.module('app', ['ionic', 'ionic.utils'])
.run(function($localstorage) {

  $localstorage.set('name', 'Max');
  console.log($localstorage.get('name'));
  $localstorage.setObject('post', {
    name: 'Thoughts',
    text: 'Today was a good day'
  });

  var post = $localstorage.getObject('post');
  console.log(post);
});

for Ios to not violate their icloud backup guidlines you have to set

<!-- config.xml -->

<?xml version='1.0' encoding='utf-8'?>
<widget ...>
  <preference name="BackupWebStorage" value="none" />
</widget>

In the config xml file. Also realize that local storage is never as good as a DB.

Upvotes: 0

Related Questions