dongsong1911 2016-12-12 19:50
浏览 159
已采纳

使用mPDF生成并保存多个pdf文件

I have a code to generate single PDF file using mPDF library. But when I try to generate 2 pdf at a time and try to download those files it does not generate second file.

Here is code:

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

When I call download(), then it will create annexc pdf successfully but for second resident() it will create the pdf which has data same as annexc form. Why so? What should I make the changes?

  • 写回答

2条回答 默认 最新

  • dongxun5349 2016-12-13 08:01
    关注

    Here is the answer after googling:

    unset the old HTML and mpdf object and create new one.

    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条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘