Skip to content

Instantly share code, notes, and snippets.

@simonj
Created October 30, 2019 19:39
Show Gist options
  • Select an option

  • Save simonj/394cf44373aa4016608df0e7ad67048b to your computer and use it in GitHub Desktop.

Select an option

Save simonj/394cf44373aa4016608df0e7ad67048b to your computer and use it in GitHub Desktop.

Revisions

  1. simonj created this gist Oct 30, 2019.
    39 changes: 39 additions & 0 deletions Vuejs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    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)
    },
    8 changes: 8 additions & 0 deletions backend
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    public function download()
    {
    $html = request()->data;

    $pdf = App::make('dompdf.wrapper');
    $pdf->loadHTML('<h1>Test</h1>');
    return $pdf->stream();
    }