【问题标题】:In codeigniter how to generate pdfs for each id using tcpdf and zip all pdfs for download在 codeigniter 中,如何使用 tcpdf 为每个 id 生成 pdf 并压缩所有 pdf 以供下载
【发布时间】:2017-03-26 06:38:15
【问题描述】:

我正在尝试使用 foreach 循环为每个 id 生成多个 pdf 但只有一个 pdf 被破坏,我也想将生成的 pdf 下载为 zip。

 public function PDF($applicantId) {
        $fieldinvestigationId = $this->fieldinvestigation_model->getFieldinvestigation($applicantId);   
        foreach($fieldinvestigationId as $fieldinv_id)
        {
        $id = $fieldinv_id->fieldVerificationId;
        $caseDetails=$this->fieldinvestigation_model->getCaseDetails($id);
        $data["caseDetails"] = $caseDetails[0];
        $applicantname = $caseDetails[0]->applicant_name;
        $principleId = $caseDetails[0]->principleId;
        $productId = $caseDetails[0]->productId;
        $applicantid = $caseDetails[0]->applicantId;
        $verificationTypeId = $caseDetails[0]->verificationId;
        $answerDetails=$this->fieldinvestigation_model->getQuestionDetails($principleId,$productId,$verificationTypeId) ;

    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);    


    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Astute');
    $pdf->SetTitle('Verification Details');
    $pdf->SetSubject('Verified details');
    $pdf->SetKeywords('Astute, PDF, verification, details, verified');   

    // set default header data
   // $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
    //$pdf->setFooterData(array(0,64,0), array(0,64,128)); 


    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); 

    // set margins
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    //$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);    
    $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);

    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); 


    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);  


    if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
        require_once(dirname(__FILE__).'/lang/eng.php');
        $pdf->setLanguageArray($l);
    }   


    $pdf->setFontSubsetting(true);   

    $pdf->AddPage(); 
    // Adds the content to the page
    $tbl = "";
    $tbl.= '<table cellspacing="0" cellpadding="5" border="1">';
    $tbl.="<tr>
                    <td><strong>Applicant Number:</strong></td>
                    <td>".$caseDetails[0]->application_refno."</td>
                    <td><strong>File No:</strong></td>
                    <td>".$caseDetails[0]->application_refno."</td>
            </tr>";

    $tbl.="</table> ";
    $cmp_header = "CMP NAME LTD";
    $pdf->SetFont('times', 'B', 12, '', true); 
    $pdf->Cell(0, 0, $cmp_header, 1, 1, 'C', 0, '', 0);  

    $pdf->SetFont('times', '', 10, '', true);   
    #table content write  
    $pdf->writeHTML($tbl, true, false, false, false, '');
    $stamp =base_url().'/assets/images/stamp.png';  
    $pdf->Image($stamp, '', '', '40', '40', '', '', '', false, 900, '', false, false, 1, false, false, false);
    $pdf->SetFont('times', 'B', 10, '', true); 

    $pdf->writeHTMLCell(0, 0, '', '', '', 0, 1, 0, true, '', true);     

    $pdf->writeHTMLCell(0, 0, '', '', 'Stamp & Signature', 0, 1, 0, true, '', true);  

    $pdf->Output($applicantname.'_'.$applicantid.'.pdf', 'I');      
        }
    }

如何为每个 id 生成每个 pdf 并压缩所有生成的 pdf 文件以供下载。

【问题讨论】:

    标签: codeigniter tcpdf


    【解决方案1】:

    我没有使用过 TCPDF,我使用的是 mPDF,但逻辑几乎相同。以下语句将pdf文件存放在目录中

    $pdf->Output($applicantname.'_'.$applicantid.'.pdf', 'I');  
    

    您可以循环浏览您的应用程序以创建 PDF 文件列表,例如

    // Assuming you have $applications as array of applicants
    foreach($applications as $application)
    {
      // All PDF settings goes here
      $pdf->Output($application['name'].'_'.$application['id'].'.pdf', 'I');  
    }
    

    然后按照Creating .zip file : PHP 线程创建存档,然后重定向到将下载它的存档文件路径。

    【讨论】:

      猜你喜欢
      • 2020-04-28
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      • 2015-01-19
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多