根据请求将pdf从后端传输到前端jQuery

问题描述 投票:0回答:1

如标题中所述,我正在尝试使用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在传输过程中被破坏了,但似乎找不到任何信息关于如何解决它。任何帮助表示赞赏

javascript php jquery pdf mpdf
1个回答
0
投票

尝试使用您的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();
}
© www.soinside.com 2019 - 2024. All rights reserved.