Show multiple pages of PDFs in the same PDF file

I want to add the function of when the user checked on the checkbox from the table of content, it will show the selected checkbox data and display it in the same PDF file with multiple pages when the "download selected invoice " button is clicked.

For example, each checked rows is a PDF file here. Suppose if i checked 6 rows, 6 pages of selected invoices will be shown the the same PDF file. I tried to use AJAX method to achieve this function but i do not know why it does not work. What i am trying to do here is i get the selected order_id and pass it to the download-in-bulk.php file. And i want to execute it one by one through loop. But i do not know why my code does not work. It somehow just went back to the home page of system when i did not direct it back to the home page.

Can you all help me with this? on how to show multiple pages of invoices in the same PDF file. I really need this to work for my project. Thanks in advance !

      <div class="col-12">
            <div class="card">
              <div class="card-header">
                <h4 class="card-title">Print Orders</h4>
                <?php 
                $sql = "SELECT *, ordered_items.id as orderno FROM ordered_items 
                        LEFT JOIN products ON ordered_items.product_id = products.id
                        LEFT JOIN users ON ordered_items.user_id = users.id
                        LEFT JOIN sellers ON ordered_items.seller_id = sellers.id WHERE ordered_items.order_status = 5 GROUP BY ordered_items.order_id
                        ORDER BY ordered_items.order_datetime DESC ";
                $query = $conn->query($sql);
                $tot_orders = mysqli_num_rows($query);
                ?>
                <b class="float-left mt-4">Total Orders ( <span class="text-success">Completed </span>): <?php echo $tot_orders; ?></b>
                 <span>
              <div class="card-body">
                <div class="table-responsive" style="max-height: 70vh">
                  <table class="table">
                    <thead class="text-primary">
                    <th style=" position: sticky;top: 0; background: white"; >
                        <input type="checkbox" id="checkAl">
                    </th>
                      <th style=" position: sticky;top: 0; background: white";>
                        Order NO / Delivery NO
                      </th>
                      <th style=" position: sticky;top: 0; background: white";>
                        Delivery / Self Pick-up Date
                      </th>
                      <th style=" position: sticky;top: 0; background: white";>
                        Order by
                      </th>
                      <th style=" position: sticky;top: 0; background: white";>
                        Revieve Item Method 
                      </th>
                      <th style=" position: sticky;top: 0; background: white";>
                        Total Price
                      </th>
            
                      <th style="text-align:center;position: sticky;top: 0; background: white;">
                        Action
                      </th>

                    </thead>
                    <tbody id="orders">
                      <?php
                      if (!$tot_orders) {
                        echo '
                          <div class="col-12">
                            <div class="badge badge-danger">No Orders Found</div>
                          </div>
                        ';
                      } else {
                        while ($row = $query->fetch_assoc()) {
                          if($row['item_deliver_method'] == 1) {
                          $item_delivery_method = '<span >Delivery</span>';
                        }
                        if($row['item_deliver_method'] == 2) {
                          $item_delivery_method = '<span >Self-Pickup</span>';
                        }
                          $grantotal = 0.00;
                          $totsql = "SELECT*, SUM(purchase_price) as total_price FROM ordered_items 
                           LEFT JOIN invoice_price ON ordered_items.order_id = invoice_price.order_id
                        LEFT JOIN discount ON invoice_price.discount_id = discount.id  
                          WHERE ordered_items.order_id = '".$row['order_id']."' ";
                          $totquery = $conn->query($totsql);
                          $totrow = $totquery->fetch_assoc();
                          $total_purchase = $totrow['total_price'];
                          $discount_amount = $totrow['discount_amount'];
                          $round_off_value = $totrow['round_off_value'];
                          if($totrow['discount_type'] == 1){
                          $grandtotal = $total_purchase - $discount_amount + $round_off_value ;
                        }
                       else{
                          $grandtotal = $total_purchase - ($total_purchase * $discount_amount/100) + $round_off_value ;
                        }
                                                  
                      ?>

                      <tr>
                        <td>
                            <input type="checkbox" id="checkItem" name="order_id[]" class="download-bulk"value="<?php echo $row["order_id"]; ?>">
                        </td>
                        <td>
                        #<?php echo $row['order_id']; ?>
                        </td>
                        <td>
                        <?php echo $row['delivery_date']; ?>
                        </td>
                        <td>
                        <?php echo $row['user_fullname']; ?>
                        </td>
                        <td class="text-center">
                        <?php echo  $item_delivery_method; ?>
                        </td>
                         <td>
                              RM<?php echo number_format  ($grandtotal,2); ?>
                         
                        </td>
                          
                       <td class="text-left">
                          <a href="invoice.php?oid=<?php echo $row['order_id']; ?>&uid=<?php echo $row['user_id']; ?>" style="text-decoration: none;color: #000; " title="Invoice">
                         <i class="fa fa-print" aria-hidden="true"></i> Print Invoice
                          </a>
                        </br>
                        <a class="text-danger" href="delivery-receipt.php?oid=<?php echo $row['order_id']; ?>&uid=<?php echo $row['user_id']; ?>" style="text-decoration: none;color: #000;" title="Delivery Receipt">
                         <i class="fa fa-print" aria-hidden="true"></i> Print Delivery Order
                          </a>
                        </td>
                      </tr>
                      <?php 
                          }
                        } 
                      ?>
                    </tbody>
                  </table>
                </div>
              </div>
            
          </div>
        </div>
      </div>
    </div>
    <div class="row">
    					<button type="button" name="btn_download_bulk" id="btn_download_bulk" value="True" class="btn btn-danger btn-icon-split m-2">
    							<span class="icon text-white-50">
    								<i class="fa fa-trash-o"></i>
    							</span>
    							<span class="text">Download Selected Invoice</span>
    						</button>
    					</div>
      </div>


    <script>
    $("#checkAl").click(function () {
    $('input:checkbox').not(this).prop('checked', this.checked);
    });
    </script>
    <script type="text/javascript">
    $(document).ready(function(){
      $('#btn_download_bulk').click(function(){
        // if(confirm("Are you sure you want to delete these products?"))
        // {
            var id=[];

            $(':checkbox:checked').each(function(i){
              id[i] = $(this).val();

            });

            if(id.length === 0){
              alert("Please select at least one invoice");
            }else if (confirm("Are you sure you want to download these invoices?")){
                 document.location.href='download-in-bulk.php';
                $.ajax({
                  url:'download-in-bulk.php',
                  method:'POST',
                  data:{id:id},
                  success:function(){
                    for(var i=0; i<id.length; i++){
                      $('tr#'+id[i]+'').css('background-color','#ccc');
                      $('tr#'+id[i]+'').fadeOut('slow');
                    }
                  }

                });
            
        }
        else{
          return false;
        }

      });

    });
      </script>

This is my download-in-bulk.php codes

    <?php
    include '../session.php';
    require_once('tcpdf/tcpdf.php');

    if(isset($_POST["order_id"]))
    {
     foreach($_POST["order_id"] as $oid)
     {
        class MYPDF extends TCPDF {
            // Page footer
            public function Footer() {
                // Position at 25 mm from bottom
                $this->SetY(-5);
                // Set font
                $this->SetFont('helvetica', '', 9);
                $this->SetTextColor(0,0,0);
                
                $this->Cell(0, 0, 'This receipt was generated by computer no signed was needed.', 0, 0, 'C');
                $this->Ln();   
            }   
        }

        $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        //$pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);  
        $pdf->SetCreator(PDF_CREATOR);  
        $pdf->SetTitle('Invoice');
        $pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);  
        $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));  
        $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));  
        $pdf->SetDefaultMonospacedFont('helvetica');  
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);  
        $pdf->SetMargins(PDF_MARGIN_LEFT, '10', PDF_MARGIN_RIGHT);
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(TRUE);
        $pdf->SetAutoPageBreak(TRUE, 10);
        
        $pdf->SetFont('times', '', 7);
        $pdf->setCellHeightRatio(0.45);
        $pdf->AddPage();
        

    	                      
        date_default_timezone_set("Asia/Kuala_Lumpur");

    	
        $htmlcontent="";



        $isql = "SELECT *, DATE(order_datetime) as orderdate FROM ordered_items 
        LEFT JOIN products ON ordered_items.product_id = products.id 
        LEFT JOIN users ON ordered_items.user_id = users.id
        LEFT JOIN billing_address ON billing_address.user_id = users.id
        LEFT JOIN brand  ON products.product_brand = brand.id
        LEFT JOIN sellers ON ordered_items.seller_id = sellers.id
        LEFT JOIN invoice_price ON ordered_items.order_id = invoice_price.order_id
        WHERE ordered_items.order_id = '".$order_id."' AND ordered_items.user_id= '".$user_id."' GROUP BY ordered_items.order_id";
        $iquery = $conn->query($isql);
        while ($irow = $iquery->fetch_assoc()) {
          if($irow['payment_term'] == 1) {
            $payment_type = 'Cash';
          }
          if($irow['payment_term'] == 2) {
            $payment_type = 'Credit';
          }
        $htmlcontent .='

        <table cellpadding="2" width="102%" style="background-color: #000;marging: 100px;">
            <tr>
                <td style="color:#FFF;font-weight: bold;font-size: 10px;line-height: 17px;">
                    Invoice Number
                </td>
                
                <td align="right" style="color:#FFF;font-weight: bold;font-size: 10px;line-height: 17px;">
                    #'.$order_id.'
                </td>
            </tr>
        </table>
        <p></p>

            <hr width = "102%" style="color: #DBDBDB;">
           
            <tr>
                <td width="80%"  align="justify" style="font-weight: normal;font-size: 10px;line-height: 17px;color: #000000;">
                <b>'.$irow['user_fullname'].'</b> <b>(0'.$irow['user_phone'].')</b>
                <br>
                '.$irow['bill_address'].', 
                '.$irow['bill_city'].', '.$irow['bill_postcode'].', '.$irow['bill_state'].'
                </td>
                <td width="20%" align="right" style="font-weight: bold;font-size: 10px;line-height: 17px;">
                <b>'.$irow['orderdate'].'</b>
            </td>
            </tr>

        </table>
        ';
        $htmlcontent .='

        <table cellpadding="2" width="102%" style=" marging: 100px;">
            <tr>
                <td style="color:#000;font-weight: bold;font-size: 10px;line-height: 10px;">
                    Payment Term : '.$payment_type.'
                </td>
            </tr>
        </table>
        ';



        $htmlcontent .='
        <table cellpadding="1" style="background-color: #4c4646;">
        <tr>
            <td  width= "8%" style="color:#FFF;font-weight: bold;font-size: 8px;line-height: 10px;">
                No.
            </td>
            
            <td width= "60%" style="color:#FFF;font-weight: bold;font-size: 10px;line-height: 10px;">
                Product Title
            </td>
            <td width= "9%" style="color:#FFF;font-weight: bold;font-size: 10px;line-height: 10px;">
                U.Price
            </td>
            <td width= "7%" style="color:#FFF;font-weight: bold;font-size: 10px;line-height: 10px;">
                QTY
            </td>
            <td width= "7%"  style="color:#FFF;font-weight: bold;font-size: 10px;line-height: 10px;">
                UOM
            </td>
            <td width= "11%"style="color:#FFF;font-weight: bold;font-size: 10px;line-height: 10px;">
                Amount
            </td>
        </tr>
    </table>
    ';
        }


    $i = 0;
    $sql = "SELECT * FROM ordered_items 
    LEFT JOIN products ON ordered_items.product_id = products.id 
    WHERE ordered_items.order_id = '".$order_id."' ";
    $query = $conn->query($sql);
    while ($row = $query->fetch_assoc()) {
                $htmlcontent .='
                <table id="receiptcontent" cellpadding="1" style="max-height: 1000px;  ">
                <tr>
                <hr width = "102%">
                        <td  width= "4%"  style="font-weight: normal;font-size: 8px;line-height: 8px;color: #000000;">
                        '. ++$i .' .
                        </td>
                        <td width= "64%"  style=" font-weight: normal;font-size: 8px;line-height: 8px;color: #000000;">
                        '.$row['product_title'].'
                        </td>
                        <td width= "8%"style=" text-align: justify; font-weight: normal;font-size: 8px;line-height: 8px;color: #000000;">
                        RM '.$row['product_buy_price'].'
                      </td>
                         <td width= "8%" style=" text-align: center;font-weight: normal;font-size: 8px;line-height: 8px;color: #000000;">
                           '.$row['quantity'].' 
                        </td>
                        <td width= "7%" style=" text-align: center;font-weight: normal;font-size: 8px;line-height: 8px;color: #000000;">
                        '.$row['quantity_unit'].'
                     </td>
                     <td width= "4%" style="font-weight: normal;font-size: 8px;line-height: 8px;color: #000000;">
                            RM 
                        </td>
                        <td width= "6%"style="text-align: right; font-weight: normal;font-size: 8px;line-height: 8px;color: #000000;">
                            '.$row['purchase_price'].'
                        </td>
                       
                    </tr>
                    
                </table>
            
                ';
    }

    $totsql = "SELECT SUM(purchase_price) as total_price , order_id  FROM ordered_items 
    WHERE order_id = '".$order_id."' ";
    $totquery = $conn->query($totsql);
    while ($totrow = $totquery->fetch_assoc()) {

                $htmlcontent .='
                <p></p>
                <table cellpadding="3" style="background-color: #4c4646;">
                    <tr>
                        <td width="86.8%" style="color:#FFF;font-weight: bold;font-size: 10px;line-height: 10px;">
                         Sub Price
                        </td>
                        <td width="6%" style="color:#FFF;font-weight: bold;font-size: 10px;line-height: 10px;">
                         RM
                        </td>
                        <td width="10%"align="right" style="color:#FFF;font-weight: bold;font-size: 10px;line-height: 10px;">
                        '.number_format($totrow['total_price'],2).'
                        </td>
                    </tr>
                </table>
                <br>
                ';
    }


    $sub_price = 0.00;
    $total_price = 0.00;
    $sql1="SELECT *, SUM(purchase_price) as ordertotalprice FROM ordered_items 
    LEFT JOIN invoice_price ON ordered_items.order_id = invoice_price.order_id
    LEFT JOIN discount ON invoice_price.discount_id = discount.id
    WHERE ordered_items.order_id = '".$order_id."' ";
    $query1 = $conn->query($sql1);
    $row1 = $query1->fetch_assoc();
    $discount_price = $row1['discount_amount'];
    $price = $row1['ordertotalprice'];
    if($row1['ordertotalprice'] < 500.00 && $row1['item_deliver_method'] == 1){
    $deliveryfee = 10.00;
    $total_price =  $sub_price + $deliveryfee;
    }else{
    $deliveryfee = 0.00;
    $total_price =  $sub_price + $deliveryfee;
    }
    if($row1['discount_type'==1]){
    $deduct_price = $discount_price;

    }else{
        $disprice = $price * ($discount_price/100);
        $deduct_price = $disprice;
    }

    $query1 = $conn->query($sql1);
    while ($row1 = $query1->fetch_assoc()) {

                $htmlcontent .='
                <p></p>
                <table cellpadding="1">
                    <tr>
                    <td width= "12.5%" style="color:#000;font-size: 8px;line-height: 2px;">
                    </td>
                       <td width= "75%" style="color:#000;font-size: 8px;line-height: 2px;">
                           Delivery Fees
                       </td>
                       <td width= "9.5%" style="color:#000;font-size: 8px;line-height: 2px;">
                           RM
                       </td>
                        <td width= "5%"  align="right" style="color:#000;font-size: 8px;line-height: 2px;">
                           '.number_format($deliveryfee,2).'
                        </td>
                    </tr>
            
                </table>
                <br>
                <p></p>
                <table cellpadding="1">
                    <tr>
                     <td width= "12.5%" style="color:#000;font-size: 8px;line-height: 2px;">
                     </td>
                        <td width= "75%" style="color:#000;font-size: 8px;line-height: 2px;">
                            Discount ('.$row1['discount_name'].')
                        </td>
                        <td width= "9.5%" style="color:#000;font-size: 8px;line-height: 2px;">
                            RM
                        </td>
                        <td width= "5%" align="right" style="color:#000;font-size: 8px;line-height: 2px;">
                       - '.number_format($deduct_price,2).'
                        </td>
                    </tr>
            
                </table>
                <br>
                <p></p>
                <table cellpadding="1">
                <tr>
                <td width= "12.5%" style="color:#000;font-size: 8px;line-height: 2px;">
                </td>
                   <td width= "75%" style="color:#000;font-size: 8px;line-height: 2px;">
                       Round off
                   </td>
                   <td width= "9.5%" style="color:#000;font-size: 8px;line-height: 2px;">
                       RM
                   </td>
                   <td width= "5%" align="right" style="color:#000;font-size: 8px;line-height: 2px;">
                      '.$row1['round_off_value'].'
                    </td>
                    </tr>
            
                </table>
                <br>
                ';
    }


    $sub_price2 = 0.00;
    $total_price2 = 0.00;
    $sql2="SELECT *, SUM(purchase_price) as grandtotal FROM ordered_items 
    LEFT JOIN invoice_price ON ordered_items.order_id = invoice_price.order_id
    LEFT JOIN discount ON invoice_price.discount_id = discount.id
    WHERE ordered_items.order_id = '".$order_id."' ";
    $query2 = $conn->query($sql2);
    $row2 = $query2->fetch_assoc();
    $sub_price2 = $row2['grandtotal'] - $deduct_price + $row2['round_off_value']  ;
    if($row2['grandtotal'] < 500.00 && $row2['item_deliver_method'] == 1){
    $deliveryfee = 10.00;
    $total_price2 =  $sub_price2 + $deliveryfee;
    }else{
    $deliveryfee = 0.00;
    $total_price2 =  $sub_price2 + $deliveryfee;
    }
    $query2 = $conn->query($sql2);
    while ($row2 = $query2->fetch_assoc()) {

                $htmlcontent .='
                <p></p>
                <table cellpadding="3" style="background-color:  #000">
                    <tr>
                        <td width="86.8%" style="color:#FFF;font-weight: bold;font-size: 10px;line-height: 10px;">
                         Total Price
                        </td>
                        <td width="6%" style="color:#FFF;font-weight: bold;font-size: 10px;line-height: 10px;">
                         RM
                        </td>
                        <td width="10%"align="right" style="color:#FFF;font-weight: bold;font-size: 10px;line-height: 10px;">
                           '.number_format($total_price2,2).'
                         
                        </td>
                    </tr>
                </table>
                <br>
                ';
    }

        $pdf->IncludeJS($js);
        $pdf->writeHTML($htmlcontent);  
        ob_end_clean(); 
        $pdf->Output('invoice.pdf', 'D');
     
     }
    }

    ?>

Well, if you have many checkboxes you would have to check them and loop thru them.
And, make a separate PDF for each or at least put a “new-page” line between each.
In tcPDF, you can add a pagebreak like this:

$this->checkPageBreak($this->PageBreakTrigger + 1);

You would have to add it after each invoice page but, NOT the last one. Or before each page except the first one. Hope this helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service