Shoutout to @MissakBoyajian for the assistance! Here's how I tackled the issue:
First, I installed the File Transfer and File plugins for Ionic native.
this.platform.ready().then(() => {
const fileTransfer: FileTransferObject = this.transfer.create();
const pdfLocation = this.pdffile; // pdffile is the PDF URL obtained from the generatePDF API response
fileTransfer.download(pdfLocation, this.storageDirectory + "filename.pdf").then((entry) => {
const alertSuccess = this.alertCtrl.create({
title: `Download Succeeded!`,
subTitle: `PDF successfully downloaded to: ${entry.toURL()}`,
buttons: ['Ok']
});
alertSuccess.present();
this.file.readAsDataURL(this.storageDirectory, 'filename.pdf')
.then((datafile) =>{
this.attachpdf(id,datafile);
})
.catch((err) =>{
console.log("Error: "+err);
});
}, (error) => {
const alertFailure = this.alertCtrl.create({
title: `Download Failed!`,
subTitle: `PDF download unsuccessful. Error code: ${error.code}`,
buttons: ['Ok']
});
alertFailure.present();
});
});
I utilized a function I found through research to convert base64 data into a blob.
public dataURItoBlob(dataURI) {
// logic to convert dataURI to blob...
}
After that,
public attachpdf(emailid,filetoattach){
let headers = new Headers();
headers.append(...);
headers.append('enctype','multipart/form-data');
var blob = this.dataURItoBlob(filetoattach);
var data ={... };
var formData = new FormData();
formData.append("data",JSON.stringify(data));
formData.append("doc",blob);
this.http.post('sendMail API',formData, {headers: headers})
.map(res => res.json())
.subscribe(results => {
...
},
error=>{
..
}
)
}
Success at last!