2017-09-06 59 views
1

Ajax调用MPDF生成PDF这个问题可能是重复的,但我没有找到一个有效的答案,这种类型的错误。我的代码运行良好,而不使用ajax,但如果使用ajax会产生错误。使用在codeginiter

$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"); 

谁能懂得这方面的工作?

+0

什么错误ü[R得到些什么? –

+0

发布错误请求 –

+0

仅供参考,如果您通过ajax调用该函数,则不起作用。因为视图不会通过Ajax调用 –

回答

1
  1. 在$ pdf-> Output()中使用“F”;
  2. 您保存
  3. 返回保存的文件名,并在Ajax调用该URL。
$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.'filename.pdf'

;

0

请更换

$pdf->Output($pdfFilePath, "D"); 

$pdf->Output($pdfFilePath, "F"); 

因为D是下载和Ajax将不提供用户界面下载文件,以便把F保存 和文件路径应该是有效的

$pdfFilePath ="Salesreport-".time()."-download.pdf"; 

$pdfFilePath =FCPATH."upload/Salesreport-".time()."-download.pdf"; 

因此它会按给定名称保存到upload文件夹中。

注:不能下载阿贾克斯它会给错误。