Search code examples
phpmysqlfpdf

PHP fpdf in while loop - I want to split into multiple pdf


I have this code below. How to generate multiple PDF's depending on the data of a while loop? Right now it generates only one PDF. Example if there are 5 employees it should generate also 5 PDF's with each PDF have unique employee name.Thanks Much.

<?php session_start() ; ?>
<?php include "../db_connection301.php" ; ?>
<?php include "../db_connection302.php" ; ?>
<?php include "../db_connection303.php" ; ?>
<?php include "../db_connection304.php" ; ?>
<?php include "../db_connection305.php" ; ?>
<?php

 require_once('../../bus_partners/fpdf/fpdf/fpdf.php');
 require_once('../../bus_partners/fpdf/fpdi/fpdi.php');

 $pdf = new FPDI();


 $query_data = "SELECT * FROM alphalist_compen_2019";
 $result_alphalist = mysqli_query($connection303, $query_data ) ;   
 while ($row_alphalist = mysqli_fetch_array($result_alphalist)) {


         $ee_numbers           =   $row_alphalist['EE_NUMBER'] ;
         $last_name            =   $row_alphalist['LAST_NAME'] ;
         $first_name           =   $row_alphalist['FIRST_NAME'] ;
         $employee_name        =   $first_name." ".$last_name ; 



         $pageCount    =  $pdf->setSourceFile('C:/wamp64/www/dbsmypay.com/bus_partners/BIR-forms/2316-new.pdf') ;
         $tplIdx       =  $pdf->importPage(1, '/MediaBox') ;


         $pdf->SetAutoPageBreak(false) ;
         $pdf->addPage('P', 'Legal') ; // SETTING THE PAPER SIZE OF THE PDF
         $pdf->useTemplate($tplIdx, 8, 12, 200, 320) ; // SETTING THE MARGIN LEFT TOP RIGHT BOTTOM


         $pdf->SetFont('Arial') ;
         $pdf->SetFontSize(10) ;
         $pdf->SetTextColor(12, 12, 12) ;



         $pdf->SetXY(46, 283) ;
         $pdf->Write(0, $employee_name) ;


         }


         $file_location = "C:/wamp64/www/dbsmypay.com/per-day/bir-2316/2019/".$ee_numbers.".pdf" ;
         $pdf->Output($file_location,'F') ;

Solution

  • Put your $pdf = new FPDI(); and your $pdf->Output($file_location,'F') ; in the while loop like this:

    <?php session_start() ; ?>
    <?php include "../db_connection301.php" ; ?>
    <?php include "../db_connection302.php" ; ?>
    <?php include "../db_connection303.php" ; ?>
    <?php include "../db_connection304.php" ; ?>
    <?php include "../db_connection305.php" ; ?>
    <?php
    
     require_once('../../bus_partners/fpdf/fpdf/fpdf.php');
     require_once('../../bus_partners/fpdf/fpdi/fpdi.php');
    
    
     $query_data = "SELECT * FROM alphalist_compen_2019";
     $result_alphalist = mysqli_query($connection303, $query_data ) ;   
     while ($row_alphalist = mysqli_fetch_array($result_alphalist)) {
    
             $pdf = new FPDI();
    
             $ee_numbers           =   $row_alphalist['EE_NUMBER'] ;
             $last_name            =   $row_alphalist['LAST_NAME'] ;
             $first_name           =   $row_alphalist['FIRST_NAME'] ;
             $employee_name        =   $first_name." ".$last_name ; 
    
    
    
             $pageCount    =  $pdf->setSourceFile('C:/wamp64/www/dbsmypay.com/bus_partners/BIR-forms/2316-new.pdf') ;
             $tplIdx       =  $pdf->importPage(1, '/MediaBox') ;
    
    
             $pdf->SetAutoPageBreak(false) ;
             $pdf->addPage('P', 'Legal') ; // SETTING THE PAPER SIZE OF THE PDF
             $pdf->useTemplate($tplIdx, 8, 12, 200, 320) ; // SETTING THE MARGIN LEFT TOP RIGHT BOTTOM
    
    
             $pdf->SetFont('Arial') ;
             $pdf->SetFontSize(10) ;
             $pdf->SetTextColor(12, 12, 12) ;
    
    
    
             $pdf->SetXY(46, 283) ;
             $pdf->Write(0, $employee_name) ;
             $file_location = "C:/wamp64/www/dbsmypay.com/per-day/bir-2316/2019/".$ee_numbers.".pdf" ;
             $pdf->Output($file_location,'F') ;
    
             }