使用mPDF

时间:2016-12-12 19:50:04

标签: php codeigniter pdf codeigniter-3 mpdf

我有一个使用mPDF库生成单个PDF文件的代码。但是,当我尝试一次生成2个pdf并尝试下载这些文件时,它不会生成第二个文件。

这是代码:

function download($id)
    {
        $this->load->library('m_pdf');

        // download annexC form
        $this->annexc($id);

        // download resident form
        $this->resident($id);
    }

function annexc($id)
    {
        $data1['udata'] = $this->general_model->getUserDetail($id);
        // mprd($data);
        $name1 = str_replace(' ', "-", strtolower($data1['name']));
        $time1 = time();
        $file_name1 = $name1.$time1.$id.'-Annexc-Form';
        $html1 = $this->load->view('annexc/form_download', $data1, true);
        $pdfFilePath1 = SAVE_PDF_ANNEXC.$file_name1.".pdf";
        $this->m_pdf->pdf->SetDisplayMode('fullpage');
        $this->m_pdf->pdf->WriteHTML($html1);
        $this->m_pdf->pdf->Output(SAVE_PDF_ANNEXC.$file_name1.".pdf", "F");
    }

    function resident($id)
    {
        $data['udata'] = $this->general_model->getUserDetail($id);
        $name = str_replace(' ', "-", strtolower($data['name']));
        $time = time();
        $file_name = $name.$time.$id.'-Residential-Form';
        $html = $this->load->view('resident/form_download', $data, true);
        $file_name = trim($file_name);
        $pdfFilePath = SAVE_PDF_RESIDENT.$file_name.".pdf";
        $this->m_pdf->pdf->SetDisplayMode('fullpage');
        $this->m_pdf->pdf->defaultfooterfontstyle='';
        $this->m_pdf->pdf->defaultfooterfontsize=12;
        $this->m_pdf->pdf->defaultfooterline=0;
        $this->m_pdf->pdf->setFooter('{PAGENO}');
        $this->m_pdf->pdf->WriteHTML($html);
        $this->m_pdf->pdf->Output($pdfFilePath, "F");
    }

当我调用download()时,它会成功创建annexc pdf,但对于第二个驻留(),它将创建pdf,其数据与annexc形式相同。为什么这样?我该怎么做才能做出改变?

2 个答案:

答案 0 :(得分:2)

以下是谷歌搜索后的答案:

取消设置旧的HTML和mpdf对象并创建新对象。

function download($id)
    {
        $data['udata'] = $this->general_model->getUserDetail($id);
        $time = time();
        ini_set('memory_limit','256M');
        $file_name_res = $time.$id.'-resident-form';
        $html = $this->load->view('resident/form_download', $data, true);
        $pdfFilePathRes = PDF_FILES.$file_name_res.".pdf";
        $this->load->library('m_pdf');
        $mpdf = new mPDF();
        $mpdf->SetDisplayMode('fullpage');
        $mpdf->defaultfooterfontstyle='';
        $mpdf->defaultfooterfontsize=12;
        $mpdf->defaultfooterline=0;
        $mpdf->setFooter('{PAGENO}');

        $mpdf->WriteHTML($html);
        $mpdf->Output($pdfFilePathRes, "F");

        unset($mpdf); // this is the magic
        unset($html); // this is the magic

        ini_set('memory_limit','256M');
        $file_name = $time.$id.'-annexc-form';
        $html = $this->load->view('annexc/form_download', $data, true);
        $pdfFilePath = PDF_FILES.$file_name.".pdf";
        $this->load->library('m_pdf');
        $mpdf = new mPDF();
        $mpdf->SetDisplayMode('fullpage');
        $mpdf->WriteHTML($html);
        $mpdf->Output($pdfFilePath, "F");        
    }

答案 1 :(得分:0)

您正在调用相同的模型函数

$data['udata'] = $this->general_model->getUserDetail($id);

然后用相同的结果回显相同的结果

$html1 = $this->load->view('annexc/form_download', $data1, true);
$html = $this->load->view('resident/form_download', $data, true);

即使视图的样式不同,您也会得到相同的结果。