#Fetching a PDF file from API

17 messages ยท Page 1 of 1 (latest)

harsh yacht
#

shouldn't you use atob in that case? ๐Ÿค”

sly fjord
#

yea, that was an error on my part (i tried different things), but i'm still getting the invalid character

#

let me get you the base64 response and the console.log of it

#

for some reason i'm getting these weird characters

#

it seems like these weird characters are already in the encoded version ๐Ÿค”

#

Maybe the problem comes from my call to the API / to the API itself

harsh yacht
#

not proficient in that regard, but you could try explicitly converting it to base64 with btoa on your server side before applying atob at your client

sly fjord
#

I'm not the one who is in charge of the API unfortunately, where shoud I do this conversion ?

harsh yacht
#

I checked the string you sent and the characters you pointed out are just not renderable in the font you are using. They are not meant to be read by humans anyway, but stored in a pdf file and viewed with appropriate software

sly fjord
#

Interesting, I suppose that a PDF converter library can work with these

harsh yacht
#

I mean... it's literally the contents of a PDF file. When you open any PDF file you have lying around with a text editor you will see equally scrambled content. You said yourself the API provides a PDF file, so it is no wonder you can not read it in plain text. It is meant to be saved as .pdf.

sly fjord
#

The thing is that I wanted to use an array buffer to create a blob object to recreate the PDF temporarily to show it to the user

sly fjord
#

I've found a workaround : not use $.ajax() for this solution since it doesn't support blob responses, i use the good'old XMLHttpRequest :

harsh yacht
#

how about fetch?