Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert byte array to pdf and download

I am trying to do a simple task of downloading a http response to a pdf. I am generating a pdf file but I am getting an error of "Failed to open PDF".

Here is what the response looks like. screenshot

And here is what I am doing.

 let blob = new Blob([response.data], { type: 'application/pdf' })
 FileSaver.saveAs(blob, 'foo.pdf')
like image 317
texas697 Avatar asked Dec 06 '25 09:12

texas697


1 Answers

The string from the response seem to be Base-64 encoded (ref. fiddle). You can decode it using fetch() (or XMLHttpRequest() for older browsers):

fetch("data:application/pdf;base64," + response.data)
  .then(function(resp) {return resp.blob()})
  .then(function(blob) {
    FileSaver.saveAs(blob, 'foo.pdf')
  });

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!