Skip to content

Instantly share code, notes, and snippets.

@simonj
Created October 30, 2019 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonj/a9d99bf6428b576594b239e49a7a0fc2 to your computer and use it in GitHub Desktop.
Save simonj/a9d99bf6428b576594b239e49a7a0fc2 to your computer and use it in GitHub Desktop.
<?php
public function download()
{
$html = request()->data;
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1>');
return $pdf->stream();
}
downloadPlan() {
let formData = document.getElementById("planComponent").innerHTML
axios.post('/api/training-plans/download', {responseType: 'arraybuffer', data: formData})
.then(response => {
this.downloadFile(response, 'customFilename')
this.$toasted.show('Nu har nu downloadet planen', {type: 'success'})
}, response => {
this.$toasted.show('Planen kunne ikke downloades, prøv igen', {type: 'error'})
console.warn('error from download_contract')
console.log(response)
// Manage errors
})
},
downloadFile(response, filename) {
// It is necessary to create a new blob object with mime-type explicitly set
// otherwise only Chrome works like it should
var newBlob = new Blob([response.data], {type: 'application/pdf'})
// IE doesn't allow using a blob object directly as link href
// instead it is necessary to use msSaveOrOpenBlob
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(newBlob)
return
}
// For other browsers:
// Create a link pointing to the ObjectURL containing the blob.
const data = window.URL.createObjectURL(newBlob)
var link = document.createElement('a')
link.href = data
link.download = filename + '.pdf'
link.click()
setTimeout(function () {
// For Firefox it is necessary to delay revoking the ObjectURL
window.URL.revokeObjectURL(data)
}, 100)
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment