使用axios的pdf下载-vuejs

时间:2019-03-08 05:20:27

标签: laravel laravel-5 vue.js axios dompdf

获取::: p时出错

  

未捕获(承诺)TypeError:window.URL.createObjectURL不是eval的函数
  无法下载pdf

我的代码:

axios({                   
  url: url,                    
  method: 'GET',                    
  responseType: 'blob',                    
  headers: {                        
    'Authorization': 'Bearer ' +localStorage.getItem('token'),                        
  }                    
}).then((response) => {
  const url = window.URL.createObjectURL(new Blob([response.data]));                    
  const link = document.createElement('a');                    
  link.href = url;                    
  link.setAttribute('download', 'file.pdf'); //or any other extension                    
  document.body.appendChild(link);                    
  link.click();                    
});

后端:

public function getPDF(Request $request) {        
    $pdf = App::make('dompdf.wrapper');
    $pdf->loadHTML('<h1><center>Test</center></h1>');        
    return $pdf->download('invoice.pdf');            
}

0 个答案:

没有答案