From server I am getting the pdf response as string format, Now on client side, I want to convert it into ArrayBuffer, So that we can change again in pdf.
I am converting it into pdf, like following, but is not pdf is getting corrupted.
function str2ab(str) {
var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
var bufView = new Uint8Array(buf);
for (var i=0, strLen=str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
Above function I got from stackover flow, I change Uint16Array to Uint8Array, pdfkit uses utf-8 uncode formate..
service.getAddress(reqData).then(function(response){
if(response.data.error ===1){
toastr.error(response.data.message ,"Unsuccessful");
}else{
var uintStr = str2ab(response.data);
var file = new Blob([uintStr], {
type : 'application/pdf'
});
//trick to download store a file having its URL
var fileURL = URL.createObjectURL(file);
var a = document.createElement('a');
a.href = fileURL;
a.target = '_blank';
a.download = envelopeObj.customer_name+'_'+ 'envId_'+envelopeObj.envelope_id+'.pdf';
document.body.appendChild(a);
a.click();
}
});
To using pdf kit to convert it into string format.
add_str = render_to_string(address_template_path , {"address_dict": address_dict})
pdfkit.from_string(add_str, file_location)
address_pdf = open(file_location)
response = HttpResponse(address_pdf.read(), content_type='application/pdf') # Generates the response as pdf response.
response['Content-Disposition'] = 'inline;filename= %s' % envelope_id