I am having an issue with downloading a PDF file from my Laravel application using axios. However, the downloaded PDF is coming up empty.
I have been using DomPdf to generate the PDF and download.js to initiate the download. Below is the code I have been using:
Note: It works perfectly fine when I try to download it without making an ajax request.
In Laravel:
return PDF::loadView('print', compact('bill'))->download($bill->number.'.pdf');
JavaScript:
axios.get(`/bills/${id}/print`, {
responseType: 'blob',
Accept: 'application/pdf'
})
.then(response => {
const download = require('@/download');
// @ is an alias for my root js directory.
download(response.data, `file.pdf`, 'application/pdf');
});