Skip to content

Instantly share code, notes, and snippets.

@abzeede
Last active May 19, 2023 14:37
Show Gist options
  • Save abzeede/840ae62a3b7db5127d177ad072114265 to your computer and use it in GitHub Desktop.
Save abzeede/840ae62a3b7db5127d177ad072114265 to your computer and use it in GitHub Desktop.
download pdf file with axios
export const getPdfInvoice = async () => {
let csrfToken = document.getElementsByName('csrf-token')[0].content
try {
const resp = await axios.get('ENDPOINT', {
responseType: 'blob',
})
// create temp download url
const downloadUrl = window.URL.createObjectURL(resp.data)
// open pdf file on new tab
window.open(downloadUrl, '__blank')
// remove temp url
window.URL.revokeObjectURL(url)
return resp
} catch (e) {
throw e
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment