Am sending a request via axios to get a pdf file, The problem am receiving the pdf file as a binary code :
"%PDF-1.4\n%????\n2 0 obj\n<<\/Filter\/FlateDecode\/Length 2100>>stream\nx??....."
I was trying hard to save the file but the file was saved as an empty pdf. What is the problem with my code please.
Note please i saw few post related to such an issue but non of them works. My code:
axios.get(process.env.VUE_APP_PUBLIC_API + '/preview-my-bill?id=123456',
{
responseType: 'blob',
headers: {
'Accept': 'application/pdf',
'content-type': 'application/pdf',
'content-transfer-encoding': 'binary',
'content-disposition': 'attachment; filename=invoice_35206159.pdf'
}
}
).then(res => {
var blob = new Blob([res.data], {type: "application/pdf"});
var downloadLink = document.createElement('a');
downloadLink.target = '_blank';
downloadLink.download = 'test.pdf';
const downloadUrl = URL.createObjectURL(blob);
this.PreviewInvoice = downloadUrl;
downloadLink.href = downloadUrl;
document.body.append(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
URL.revokeObjectURL(downloadUrl);
console.log(blob)
console.log(downloadUrl)
})
My response is like this:
{
data: "Blob {size: 43985, type: 'application/json'}"
"status": 200,
"statusText": "OK",
"headers": {
"content-type": "application/json; charset=UTF-8",
"link": "<http://mydomain.local/api/>; rel=\"https://api.w.org/\""
},
"config": {
"transformRequest": {},
"transformResponse": {},
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"headers": {
"Accept": "application/pdf",
"content-transfer-encoding": "binary",
"content-disposition": "attachment; filename=invoice_35206159.pdf"
},
"method": "get",
"responseType": "blob",
},
"request": {}
}