1

I'm trying to generate multiple pdf's for each id using foreach loop but only one pdf is getting denerated and also i want to download the generated pdf as 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');      
        }
    }

how to generate each pdf for each id and zip all the generated pdf files to download.

Mudassar Khani
  • 1,469
  • 1
  • 20
  • 39
vijay kumar
  • 287
  • 2
  • 8
  • 28

1 Answers1

0

I haven't used TCPDF I use mPDF but the logic is pretty much the same. The following statement stores the pdf file in the directory

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

You can loop through you applications to create list of PDF files like

// 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');  
}

Then follow Creating .zip file : PHP thread to create an archive and then redirect to that archived file path which will download it.

Community
  • 1
  • 1
Mudassar Khani
  • 1,469
  • 1
  • 20
  • 39