Friends,
I am using file,fleopener plugins to create and open a pdf file , the data came as base64.
My Relevant code is below . There is no Error shown in console and no PDF created…
getOwnershipCertificate(lb, bid, reason) {
if(this.global1.getnetwork())
{
this.connectivityFlag = true;
this.certificateProvider.getOwnershipCertificate(lb, bid, reason)
.then(data => {
var downloadPDF: any = data[0].ownershippdf;
var base64pdf = downloadPDF;
var contentType = 'application/pdf';
const writeDirectory = this.platform.is('ios') ? this.file.dataDirectory : this.file.externalDataDirectory;
var fileName = "Ownership.pdf";
this.file.writeFile(writeDirectory, fileName, this.convertBaseb64ToBlob(downloadPDF, 'data:application/pdf;base64'), {replace: true})
.then(() => {
this.fileOpener.open(writeDirectory + fileName, 'application/pdf')
.catch(() => {
console.log('Error opening pdf file');
});
})
.catch(() => {
console.error('Error writing pdf file');
});
}).catch(err => {
console.error('ownership get error',err);
});
} else {
alert("You are offline, please be online to get ownership certificate");
}
}
blob creating function
convertBaseb64ToBlob(b64Data, contentType): Blob {
contentType = contentType || '';
const sliceSize = 512;
b64Data = b64Data.replace(/^[^,]+,/, '');
b64Data = b64Data.replace(/\s/g, '');
const byteCharacters = window.atob(b64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
return new Blob(byteArrays, {type: contentType});
}
Waiting a Fast reply. I am struggling this for 33 days now…
Thanks
Anes