$0.17

How to export HTML table to PDF

Ask a question+
2

I have a HTML table which shows the transaction history. How do I export this table to PDF exactly the same data?

    include 'dbFunctions.php';
    include 'menu.php';
    include 'css.php';
    $user_id=$_SESSION['user_id'];
    $acc_id = $_POST['acc_id'];
    $period = $_POST['period'];
    $sort = $_POST['sort'];
    $type = $_POST['type'];
    $total_debit = 0;
    $total_credit = 0;
    $date = date("l jS \of F Y h:i:s A")
    ?>
    <html><head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        </head><body>
            <div class="content">
                <h2>Transaction History</h2>
                <?php echo $date . "<br>" . "<br>";
                $query = "SELECT account_number FROM account WHERE account_id ='$acc_id' AND user_id= '$user_id'"; // where date=$period
                $result = mysqli_query($link, $query) or die(mysqli_error($link));
                while ($row = mysqli_fetch_array($result)) { $acc_num = $row ['account_number'];} ?>
                <b>RP INTERNET BANKING ACCOUNT <?php echo $acc_num ?></b> <br>
                <?php
                }
                ?>
                <center>
            <table id="table">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Transaction Code</th>
                        <th>Reference</th>
                        <th>Debit <br/> (Withdrawal)</th>
                        <th>Credit <br/> (Deposit)</th>
                    </tr>
                </thead>
                <?php
                if ($period == 'current'){
                    $query1 = "SELECT * FROM transaction WHERE account_id ='$acc_id' AND user_id= '$user_id' AND MONTH(CURDATE())= MONTH(date)";
                }
                else if ($period == 'current_first'){
                    $query1 = "SELECT * FROM transaction WHERE account_id ='$acc_id' AND user_id= '$user_id' AND date BETWEEN CURRENT_DATE - INTERVAL 2 MONTH AND CURRENT_DATE";
                }
                else if ($period == 'current_second'){
                    $query1 = "SELECT * FROM transaction WHERE account_id ='$acc_id' AND user_id= '$user_id' AND date BETWEEN CURRENT_DATE - INTERVAL 3 MONTH AND CURRENT_DATE"; 
                }
                if($type == 'credit'){ $query1 .= " AND debit IS NULL"; }
                else if ($type == 'debit'){  $query1 .= " AND credit IS NULL"; }
                else if ($type == 'both'){ $query1 .= " "; }
                if ($sort == 'latest'){ $query1 .= " ORDER BY date DESC";  }
                else if ($sort == 'earliest'){ $query1 .= " ORDER BY date ASC"; }
                else if ($sort == 'codes'){ $query1 .= " ORDER BY transaction_code ASC"; }
                $result1 = mysqli_query( $link, $query1 ) or die( mysqli_error( $link ) );
                while ($row1 = mysqli_fetch_array($result1))
                {
                    $date1 = $row1['date'];
                    $trans_code = $row1['transaction_code'];
                    $reference = $row1['reference'];
                    $debit = $row1['debit'];
                    $credit = $row1['credit'];
                    $total_debit += $debit;
                    $total_credit += $credit;
                    $date = new DATETIME($date1);
                ?>
                <tbody>
                    <tr>
                        <td><?php echo $date->format('d-m-Y'); ?></td>
                        <td><?php echo $trans_code ?></td>
                        <td><?php echo $reference ?></td>
                        <td>
                        <?php
                        if ($debit != NULL) {
                            echo "S$" . " " . $debit;
                        }
                            ?>
                        </td>
                        <td>
                            <?php
                        if ($credit != NULL) {
                            echo 'S$' . ' ' . $credit;
                        }
                            ?>
                            </td>
                            <?php
                }
                ?>
                    </tr>
                    <tr class="alt">
                        <td colspan="3" align="right"><b>Total: </b></td>
                        <td align="right"> <b>
                <?php if ($total_debit != 0 ) {
                    echo 'S$' . ' ' . $total_debit; 
                    }
                    ?>
                            </b></td>
                            <td align="right"> <b>
                    <?php if ($total_credit != 0){
                        echo 'S$' . ' ' . $total_credit;
                        }
                        ?></b></td>
                    </tr></tbody></table></center>
                <button onclick="location.href='viewTransactionHistory.php'">Back</button>
        </div>
        </body>
    </html>
add comment

1 Answer

1

One option is by using server-side code to generate a PDF from your HTML. HTML2PDF is one option for that: http://sourceforge.net/projects/phphtml2pdf/

There’s also another tool called DOMPDF: http://www.sitepoint.com/convert-html-to-pdf-with-dompdf/

add comment

Your Answer