Search code examples
phpmysqlpdfhtml2pdf

How to generate multiple pdf file PHP Mysql and HTML2PDF


I would like to generate multiple PDF files for each sample from my database with html2pdf. I have actually different tables which contain the run name with the idsample. The 12 first character is the run name and the 8 last characters are the idsample, for example:

run0112201728S52PRO

run011220178FS22LEJ

For all these tables, I want one pdf report for each idsample. I managed to create the pdf but they are all in the same pdf instead of being all in a distinct pdf. I included the pdf generation part inside the loop. Any idea how to do it? Here what I did.

<?php

// convert
        //require_once('html2pdf.class.php');
       require_once dirname(__FILE__).'/vendor/autoload.php';
        
        use Spipu\Html2Pdf\Html2Pdf;
        use Spipu\Html2Pdf\Exception\Html2PdfException;
        use Spipu\Html2Pdf\Exception\ExceptionFormatter;

    ob_start();
$tablename='run01122017'; //the name of the table
//GET idsample that we gonna cut from the result_array
$query2 = "select DISTINCT SUBSTRING(TABLE_NAME,12,8) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME like 'run01122017%'";
$result2 = mysqli_query($conn,$query2) or die ("no query");
while($row2 = mysqli_fetch_assoc($result2))
{
    $idEcht_array[] = $row2;
    
}
//Query to pick up Idpat
$FetchIdPat="SELECT Firstname, Lastname FROM PAT WHERE idEcht='".implode(",",$idEcht_array[$i])."'";

$PerformIdPAT=mysqli_query($conn,$FetchIdPat) or die(mysqli_error($conn));
while($rowIDPat =  mysqli_fetch_assoc($PerformIdPAT)) { ?>
        Patid:&nbsp;<b><?php echo $rowIDPat["Lastname"]."&nbsp;".$rowIDPat["Firstname"];?>
//}
//Generate PDF report
$content = ob_get_clean();

    

    try
    {
        
        $html2pdf = new Html2Pdf('P', 'A4', 'fr');
        $html2pdf->pdf->SetDisplayMode('fullpage');
        $html2pdf->writeHTML($content, isset($_GET['vuehtml']));
        ob_end_clean();
        //$html2pdf->Output($tablename.'pdf');
$html2pdf->Output($tablename.'-'.implode(", ", $idEcht_array[$i]).'-Report.pdf');
            
        }
        catch(HTML2PDF_exception $e) {
            echo $e;
            exit;
        }}
    ?>

Solution

  • Please put the ob_start(); and $content = ob_get_clean(); inside the while loop appropriately, and use say a variable $index to change the filename

    So, based on your original code, the revised code is:

    <?php
    
    require_once dirname(__FILE__).'/vendor/autoload.php';
            
    use Spipu\Html2Pdf\Html2Pdf;
    use Spipu\Html2Pdf\Exception\Html2PdfException;
    use Spipu\Html2Pdf\Exception\ExceptionFormatter;
    
    $tablename='run01122017'; //the name of the table
    
    //GET idsample that we gonna cut from the result_array
    $query2 = "select DISTINCT SUBSTRING(TABLE_NAME,12,8) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME like 'run01122017%'";
    $result2 = mysqli_query($conn,$query2) or die ("no query");
    while($row2 = mysqli_fetch_assoc($result2)){
        $idEcht_array[] = $row2;
    }
    
    //Query to pick up Idpat
    $FetchIdPat="SELECT Firstname, Lastname FROM PAT WHERE idEcht='".implode(",",$idEcht_array[$i])."'";
    
    $PerformIdPAT=mysqli_query($conn,$FetchIdPat) or die(mysqli_error($conn));
    
    $index=1;
    
    while($rowIDPat =  mysqli_fetch_assoc($PerformIdPAT)) { 
        ob_start();
    ?>
    
    Patid:&nbsp;<b><?php echo $rowIDPat["Lastname"]."&nbsp;".$rowIDPat["Firstname"];?>
    
    <?php
    $content = ob_get_clean();
        
           try
           {
            $html2pdf = new Html2Pdf('P', 'A4', 'fr');
            $html2pdf->pdf->SetDisplayMode('fullpage');
            $html2pdf->writeHTML($content, isset($_GET['vuehtml']));
    
            //ob_end_clean();
            //$html2pdf->Output($tablename.'pdf');
    
            $html2pdf->Output($tablename.'-'. $index . '-Report.pdf');
                
            unset($html2pdf); 
                
            }
            catch(HTML2PDF_exception $e) {
                echo $e;
                exit;
            }
    
    $index++;
    }
    
    ?>