how to convert byte array to pdf and download
13,056
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')
});
Comments
-
texas697 almost 2 years
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.
And here is what I am doing.
let blob = new Blob([response.data], { type: 'application/pdf' }) FileSaver.saveAs(blob, 'foo.pdf')
-
Admin over 6 yearsThe response there doesn't look like a PDF file (PDF is a text file and should start with
%PDF.
). Have you checked that XMLHttpRequest uses the correct URI? -
texas697 over 6 yearsI am not sure what it is. I am trying out different ways of displaying it. here is a fiddle i forked and added the response. Maybe this will help figure it out?
-
texas697 over 6 years
-
texas697 over 6 yearsis it a base64 response? That is what I assumed at first
-
Admin over 6 yearsAh Ok, the string base-64 encoded. Simply decode it via f.ex. via fetch/XMLHttpRequest
-