Since you're using ajax, basically, you need to inject an element into the dom, and automatically trigger a click on it to initiate the download.
A helper function like this is useful for that:
export default function downloadFile(data, { filename, mimeType } = {}) {
const link = document.createElement('a');
const blob = new Blob([data], {
mimeType,
});
link.style.display = 'none';
link.href = window.URL.createObjectURL(blob);
link.download = filename;
document.body.appendChild(link);
link.click();
// Some browsers may fail to download if the link is removed too quickly
setTimeout(() => {
document.body.removeChild(link);
}, 100);
}
To use just pass your response data, the desired filename to send it as, and the appropriate mimetype to the function, it'll inject an anchor link to the dom, click it, and then 100ms later remove the link.
success: function(data) {
downloadFile(data, {
filename: `Your-desired-filename.pdf`,
mimeType: 'application/pdf',
});
},
You'll have to inspect what's actually getting sent back, not sure off hand the structure of the data that's returned... but that'll be close.