Skip to content

Instantly share code, notes, and snippets.

@Akhu
Created October 11, 2021 15:33
Show Gist options
  • Save Akhu/f164de7aa523e1e8856c3756b50f4e75 to your computer and use it in GitHub Desktop.
Save Akhu/f164de7aa523e1e8856c3756b50f4e75 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]> <html class="no-js"> <!--<![endif]-->
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="">
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<!--[if lt IE 7]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="#">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<script async defer>
function download (response) {
const url = window.URL.createObjectURL(new Blob([response.data], { type: 'application/pdf' }))
const link = document.createElement('a')
const contentDisposition = response.headers['content-disposition']
let fileName = 'unknown'
if (contentDisposition) {
let fileNameMatch = contentDisposition.match(/filename="(.+)"/)
if (!fileNameMatch) {
fileNameMatch = contentDisposition.match(/filename=(.+)/)
if (fileNameMatch.length === 2) { fileName = fileNameMatch[1] }
} else if (fileNameMatch.length === 2) { fileName = fileNameMatch[1] }
}
link.href = url
link.setAttribute('download', fileName)
document.body.appendChild(link)
link.click()
link.remove()
window.URL.revokeObjectURL(url)
}
axios.get('http://localhost:8000/api/v1/orders/print-label?order_number=CPA-010380', {
responseType: 'blob',
}).then((response) => {
this.download(response);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment