我一直在尝试通过 axios 从我的 Laravel应用程序下载 PDF,但下载的文件是一个空的 pdf。
我正在使用DomPdf创建pdf并下载.js下载它是我的代码。
注意::如果我尝试在没有 ajax 请求的情况下下载它,它就可以正常工作。
拉拉维尔,
return PDF::loadView('print', compact('bill'))->download($bill->number.'.pdf');
爪哇语
``axios.get(`/bills/${id}/print` {
responseType: 'blob',Accept: 'application/pdf'
})
.then(response => {
const download = require('@/download');
// @ is just an alias for my root js directory.
download(response.data, `file.pdf`, 'application/pdf');
});``
尝试以下代码:
在Laravel控制器中:
$pdf = PDF::loadView('finance.remainingFee', compact('fee', 'time'));
$pdf->setPaper('a4' , 'portrait');
return $pdf->output();
在 Vue 文件中:
axios.get(APP_URL + `api/finance/remainingStudentFee/${fee_id}`, {responseType: 'blob'}).then(response => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'remaining_fee.pdf'); //or any other extension
document.body.appendChild(link);
link.click();
})
.catch(error => {
console.log(error);
})