如标题中所述,我正在尝试使用jquery将pdf从后端php传递到前端。
示例php代码。
public function printPdf(){
header('Content-Disposition: attachment; filename="kainos.pdf"');
$html = $this->request->post['pdf'];
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<div>Section 1 text</div>');
return $mpdf->Output();
}
function(response){
var blob=new Blob([response], { type: 'application/pdf' });
var link=document.createElement('a');
link.href=window.URL.createObjectURL(blob);
link.download="Kainos.pdf";
link.click();
}
我得到的是空pdf。
我所知道的:
数据正在正确传输,因为可以使用javascript console.log传输和查看纯html
正在生成Pdf,因为我可以将函数的内容移至其他函数并在其他页面中获取pdf,但我在那里不需要它。
我怀疑pdf在传输过程中被破坏了,但似乎找不到任何信息关于如何解决它。任何帮助表示赞赏
尝试使用您的php脚本发送更多头文件(并修改当前的头文件:]
public function printPdf(){
ob_clean();
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="kainos.pdf"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
$html = $this->request->post['pdf'];
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<div>Section 1 text</div>');
return $mpdf->Output();
}