I'm trying to download a PDF in my browser that i have inserted text into (through pdf form fields) using axios from a flask server I am running.
Server-side code:
def create_form():
# pdf buffer is a BytesIO object that contains the pdf
...
pdf_buffer.seek(0)
return send_file(pdf_buffer, as_attachment=True, attachment_filename='file.pdf', mimetype='application/pdf')
When I tested this endpoint in postman with the save and download
option, it worked correctly and saved the file with all the text overlaid on top of the original pdf after prompting me to download the file.
However, when I use axios in chrome, the pdf downloads automatically, but the pdf is blank except for the new text i have overlaid in the form fields. The original pdf content is completely gone. Here is the code that saves the PDF with axios in chrome. The problem is the same for firefox as well.
apiRequest({
apiEndpoint,
data,
responseType: 'blob',
}).then((resp) => {
let blob = new Blob([resp.data], { type: 'application/pdf' })
const url = window.URL.createObjectURL(blob);
let a = document.createElement("a");
a.href = url;
a.download = 'file.pdf';
document.body.appendChild(a);
a.click();
})
Why would this work in postman but not in chrome or firefox? The only thing I can think of is that postman's save and download
feature treats the PDF it receives differently than the axios POST response.