Friends,
I got a web service output as base 64 encoded data for PDF. I response i get the value , but no pdf output…
My Web service provider method is
getOwnershipCertificate(lb,bid,reason)
{
var obj = {wspassword: 'Ikm#itmi$$i0n', wsname: 'getOwnershipCertPDF', lb: lb, bid: bid, reason: reason };
var myData = JSON.stringify(obj);
if(this.data)
{
return Promise.resolve(this.data);
} else {
return new Promise
(
(resolve,reject) =>
{
this.http.post('http://ccc.jhj.gov.in/mobservice/index.php/sanchaya/',myData,{ headers: new HttpHeaders()
, responseType: 'json'})
//.map(res => res)
.subscribe
(res => {
resolve(res);
},
err => {console.log("oops some error on taking ownership certificate PDF base 64 encoded string");reject(err);}
);
}
);
}
}
My Controller functions are
getOwnershipCertificate(lb, bid, reason) {
this.certificateProvider.getOwnershipCertificate(lb, bid, reason)
.then(data => {
let downloadPDF: any = data[0].ownershippdf;
//let base64pdf = 'data:application/pdf;base64,' + downloadPDF;
let base64pdf = downloadPDF;
console.log("log is"+base64pdf);
let blobPdf = this.b64toBlob(base64pdf, 'application/pdf',512);
this.file.writeFile(this.file.externalRootDirectory, 'Ownership.pdf', blobPdf, { replace: true })
.then(res => {
console.log('wres', res);
this.fileOpener.open(res.toInternalURL(), 'application/pdf').then((ores) => {
console.log('ores', ores)
}).catch(err => {
console.log('open error');
});
}).catch(err => {
console.log('save error');
});
}).catch(err => {
console.error('ownership get error',err);
});
}
and
b64toBlob(b64Data, contentType, sliceSize) {
contentType = contentType || '';
sliceSize = sliceSize || 512;
let byteCharacters = atob(b64Data);
let byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
let slice = byteCharacters.slice(offset, offset + sliceSize);
let byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
let byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var blob = new Blob(byteArrays, {type: contentType});
return blob;
}
My sample base 64 output is : Sample pdf base 64 value
I did not any error and got the base 64 encoded data as output. Please advise how to download pdf and open it default…
Thanks
Anes