Save PDF from base64 to file


#1

Hi! I have an Ionic App, and I need save a PDF file in the phone directory. The file itself is in base64, stored in the localstorage. I check every Google result and all methods fails, either the PDF wont open at all or the pages are blank. I’m out of ideas right now.

Here is the code:

/*tempDoc it's a string with the base64*/
var pathFile = "";
var fileName = "report.pdf";
if (ionic.Platform.isIOS()) {
	var pathFile = cordova.file.documentsDirectory
} else {
	var pathFile = cordova.file.externalDataDirectory
}

$cordovaFile.writeFile(pathFile, fileName, tempDoc, true)
	.then(function(success) {
		console.log("Exito guardando.")
		console.log(tempDoc);
	}, function(error) {

		alert("errore nella creazione del report")

	});

#2

It’s definitely working in android and then you have already did this concept please give the ios format

let downloadPDF: any = this.Attachment;

  fetch('data:application/pdf;base64,' + downloadPDF,
    {
      method: "GET"
    }).then(res => res.blob()).then(blob => {
      this.file.writeFile(this.file.externalDataDirectory, this.Name +"_"+this.PayMonth + "_" + this.PayPeriod+"_" + this.PayYear+".pdf", blob, { replace: true }).then(res => {
        this.fileOpener.open(
          res.toInternalURL(),
          'application/pdf'
        ).then((res) => {
          this.catchAlert(JSON.stringify(res));
        }).catch(err => {
          this.catchAlert(JSON.stringify(err));
        });
      }).catch(err => {
        this.catchAlert(JSON.stringify(err));
 });
    }).catch(err => {
      this.catchAlert(JSON.stringify(err));
    });