Search code examples
phpmysqlpdfhtml2pdf

How to generate at once multiple PDFs with html2pdf


I want to generate at once multiple separate PDF files for each idsample contained in my MySQL database. I manage to generate one PDF without any issue but the others PDF are not generated. For example, I have 2 tables: run0112201728S52PRO, run011220178FS22LEJ and I want a separate PDF file for these 2 idsamples: 28S52PRO and 8FS22LEJ

my idsample is contained in the table name, so first, I query and substring to get the idsample that I store into an array. Then, I loop for each idsample to get the Lastname and First name.

Here what I have done:

<?php
ob_start();
//Connect to MySQL database
require_once 'configphp';

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

//define the name of the tables
$tablename='run01122017';

//GET idecht 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");

//We store idsample into an array
while($row2 = mysqli_fetch_assoc($result2))
{
    $idEcht_array[] = $row2;
        
}

$index=1;

$arraysizeECHT=sizeof($idEcht_array);
for($i=0;$i<=$arraysizeECHT-1;$i++){



//Query to pick up Idsample information from PAT table
$FetchIdPat="SELECT Lastname, Firstname FROM PAT WHERE idEcht='".implode(",",$idEcht_array[$i])."'";
$PerformIdPAT=mysqli_query($conn,$FetchIdPat) or die(mysqli_error($conn));
?>
<h2>TITLE OF THE DOC</h2>
<hr/>
        <?php
        while($rowIDPat =  mysqli_fetch_assoc($PerformIdPAT)) { 
        ?>
        Pat:&nbsp;<?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']));
    $content=ob_end_clean();
    $html2pdf->Output($tablename.'-'.implode(",",$idEcht_array[$i]).'-Report.pdf');
        
        unset($html2pdf);
        
    }
    catch(HTML2PDF_exception $e) {
        echo $e;
        exit;
    }

    $index++;
} //end of the while
} //end of the for
mysqli_close($conn);
?>

Solution

  • You speak about download: keep in mind that http protocol allow one download per request.

    If your intention is to:

    1. do the request
    2. generate all pdf
    3. start all the N downloads

    This is not possible.

    Try to debug by verifies that:

    • first query generate multiple result
    • $html2pdf->Output() uses different filenames

    Here follow the cleaned code

    <?php
    //Connect to MySQL database
    require_once 'configphp';
    
    require_once __DIR__ . '/vendor/autoload.php';
    
    use Spipu\Html2Pdf\Html2Pdf;
    use Spipu\Html2Pdf\Exception\Html2PdfException;
    use Spipu\Html2Pdf\Exception\ExceptionFormatter;
    
    //define the name of the tables
    $tablename = 'run01122017';
    
    //GET idecht that we gonna cut from the result_array
    $query2 = "select DISTINCT SUBSTRING(TABLE_NAME,12,8) as idEcht FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME like '$tablename%'";
    $result2 = mysqli_query($conn, $query2) or die ("no query");
    while ($row2 = mysqli_fetch_assoc($result2)) {
    //Query to pick up Idsample information from PAT table
        $idEcht = $row2['idEcht'];
        $FetchIdPat = "SELECT Lastname, Firstname FROM PAT WHERE idEcht='$idEcht'";
        $PerformIdPAT = mysqli_query($conn, $FetchIdPat) or die(mysqli_error($conn));
        $content = "<h2>TITLE OF THE DOC</h2>\n<hr/>\n";
        while ($rowIDPat = mysqli_fetch_assoc($PerformIdPAT)) {
            $content .= "Pat:&nbsp;" . $rowIDPat["Lastname"] . "&nbsp;" . $rowIDPat["Firstname"] . "\n";
            try {
                $html2pdf = new Html2Pdf('P', 'A4', 'fr');
                $html2pdf->pdf->SetDisplayMode('fullpage');
                $html2pdf->writeHTML($content, isset($_GET['vuehtml']));
                $content = ob_end_clean();
                $html2pdf->Output("$tablename-$idEcht-Report.pdf");
                unset($html2pdf);
            } catch (HTML2PDF_exception $e) {
                echo $e;
                exit;
            }
        } //end of the while $PerformIdPAT
    } //end of the while $result2
    mysqli_close($conn);