Created
October 30, 2019 19:39
-
-
Save simonj/394cf44373aa4016608df0e7ad67048b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function download() | |
{ | |
$html = request()->data; | |
$pdf = App::make('dompdf.wrapper'); | |
$pdf->loadHTML('<h1>Test</h1>'); | |
return $pdf->stream(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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