1

I am trying to generate a PDF from binary using JavaScript.

This is the code I have:

downloadPDF(response) {        
    var binaryData = [];
    binaryData.push(response);
    var file = window.URL.createObjectURL(new Blob(binaryData, {type: "application/pdf"}))
    var a = document.createElement("a");
    a.href = file;
    a.download = "detailPDF";
    document.body.appendChild(a);
    a.click();
    window.onfocus = function () {                     
      document.body.removeChild(a)
    }
}

The issue is that it the PDF is always empty. I know the binary is valid, as I am able to generate a valid PDF using this binary in other ways.

Any ideas?

Here is the binary being passed in

UPDATE:

Below is a snip of a diff of binary in the PDF generated by my code (left), and the working PDF (right). Most of the file is the same, except a few characters for example my code has:

x��Y�k�0

Where as the working PDF has

xœíYÝkÛ0

So maybe it is the tool that is converting my string to binary array?

enter image description here

Alex
  • 3,730
  • 9
  • 43
  • 94

0 Answers0