Download PDF from binary string in Javascript

Viewed 1989

Im trying to download a PDF file from binary string, which I receive as a response from an Ajax.

I receive the following data (binaryString):

%PDF-1.4....
.....
....content of file
....
%% EOF

I tried this:

    var blob=new Blob([binaryString], {type: "application/pdf"});// change resultByte to bytes
    var link=document.createElement('a');
    link.href=window.URL.createObjectURL(blob);
    link.download="myFileName.pdf";
    link.click();

And also tried using the download.js library:

download(binaryString, "file.pdf", "application/pdf");

However, both return a PDF with the correct number of pages, but completely blank. enter image description here

Result of API test with insomnia: enter image description here

1 Answers

The binary string arrives at the corrupted front, so I decided to convert it to base64 on the back end and send it like this, but that's because I have autonomy from the back end, I don't know how it would be resolved for a non-public api and other cases.

Related