2

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
5
  • Found a thread that might help you stackoverflow.com/questions/6965107/… Commented Aug 18, 2016 at 10:35
  • @ManojSureddi I tried this link, but it is not working.. Commented Aug 18, 2016 at 10:45
  • "I change Uint16Array to Uint8Array", @geeks why exactly did you do that? do you understand the difference? How much do you know about file-encodings? Commented Aug 18, 2016 at 11:13
  • @Thomas I tried both, but not working, I change because pdfkit uses utf-8 encoding format ... Commented Aug 18, 2016 at 11:17
  • @geeks, well JS uses UCS-2, and an ArrayBuffer is just a collection of bytes without meaning (on their own). From your code I'm not sure wich one you get in your response string, utf-8-encoded bytes or the correct "chars". And since I have no experience with Blobs and a.download, I don't know what output you need. The only thing I'm pretty sure is, that you somehow have to transcode this thing; not just push the last 8-Bit of the charCodes into an Uint8Array, leaving the other half of the ArrayBuffer empty Commented Aug 18, 2016 at 11:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.