在codeigniter中如何使用tcpdf为每个id生成pdf并压缩所有pdf以供下载

时间:2017-03-26 06:38:15

标签: codeigniter tcpdf

我尝试使用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文件以供下载。

1 个答案:

答案 0 :(得分:0)

我没有使用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个帖子创建一个存档,然后重定向到将要下载的存档文件路径。