cesarvr / pdf-generator

Cordova plugin to generate pdf in the client-side
MIT License
107 stars 61 forks source link

Saving Base64 #23

Closed davidtweaver closed 7 years ago

davidtweaver commented 7 years ago

Hello,

Sorry for bugging you with issues - I'm now having trouble writing the Base64 generated by the plugin to a file - all I get are blank PDFs

This is what I'm doing to try and turn the Base64 into a PDF file that I can save on an iPad. I've tested it with other Base64 from elsewhere and had it working fine though.

Here's my code:

let thisFile = this.b64toBlob(success);

            this.file.writeFile(this.file.documentsDirectory, 'mytest.pdf', thisFile, true).then(_ => console.log('Should have successfully created file')).catch(err => console.log('File not created'));

b64toBlob(b64Data) {
    let contentType = "application/pdf"
    //contentType = contentType || '';
    var sliceSize = 512;
    b64Data = b64Data.replace(/^[^,]+,/, '');
    b64Data = b64Data.replace(/\s/g, '');
    var byteCharacters = window.atob(b64Data);
    var byteArrays = [];

    for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
      var slice = byteCharacters.slice(offset, offset + sliceSize);

      var byteNumbers = new Array(slice.length);
      for (var i = 0; i < slice.length; i++) {
        byteNumbers[i] = slice.charCodeAt(i);
      }

      var byteArray = new Uint8Array(byteNumbers);

      byteArrays.push(byteArray);
    }
    console.log('byteArrays ',byteArrays)
    var blob = new Blob(byteArrays, { type: contentType });
     //console.log('blob ', blob)
    return blob;
  }
davidtweaver commented 7 years ago

So sorry, turns out it was related to how I dealt with the blob - not related to your plugin.