Vue + Laravel: How to properly download a PDF file?

前端 未结 4 1287
野趣味
野趣味 2020-12-15 06:57

THE SITUATION:

Frontend: Vue. Backend: Laravel.

Inside the web app I need to let the user download certain pdf files:

  • I need L
4条回答
  •  青春惊慌失措
    2020-12-15 07:35

    it's works for me.

    from laravel backend:

    $pdf = PDF::loadView('your_view_name', ['data' => $data]);
    return $pdf->output();
    

    from vuejs frontend:

    axios({
    url: 'http://localhost:8000/api/your-route',
    method: 'GET',
    responseType: 'blob',
    }).then((response) => {
         var fileURL = window.URL.createObjectURL(new Blob([response.data]));
         var fileLink = document.createElement('a');
         fileLink.href = fileURL;
         fileLink.setAttribute('download', 'file.pdf');
         document.body.appendChild(fileLink);
         fileLink.click();
    
    });
    

提交回复
热议问题