这个问题可能是重复的,但我没有找到此类错误的有效答案。我的代码在不使用 ajax 的情况下运行良好,但如果使用 ajax 则会出现错误。
$this->load->library('m_pdf');
$this->data['title']="Sales Report PDF";
$this->data['description']="Sales report in PDF format";
$data = $this->get_sales_report_for_pdf();
$html=$this->load->view('dashboard/sales_report_pdf',$data, true);
$pdfFilePath ="Salesreport-".time()."-download.pdf";
$pdf = $this->m_pdf->load();
$pdf->WriteHTML($html,2);
$pdf->Output($pdfFilePath, "D");
有人知道如何解决这个问题吗?
最佳答案
- 在 $pdf->Output() 中使用“F”;
- 返回您保存的文件名,并在 ajax 中调用您保存该文件的网址。
$this->load->library('m_pdf'); $this->data['title']="Sales Report PDF"; $this->data['description']="Sales report in PDF format"; $data = $this->get_sales_report_for_pdf(); $html=$this->load->view('dashboard/sales_report_pdf',$data, true); $pdfFilePath ="Salesreport-".time()."-download.pdf"; $pdf = $this->m_pdf->load(); $pdf->WriteHTML($html,2); $pdf->Output(FCPATH.$pdfFilePath, "F");
return $pdfFilePath;
在 Ajax 响应中 location.href = YOURPATH.'文件名.pdf';
关于jquery - 在 codeigniter 中使用 ajax 调用 mpdf 生成 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46071941/