将HTML表导出为PDF

时间:2014-01-23 16:54:58

标签: php html css pdf

我有一个HTML表格,显示用户拥有的帐户的交易历史记录。如何将此表导出为PDF完全相同的数据和相同的表格样式(css)?我是否需要为PDF使用第三方?

<?php
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">
                <style>
            #table
            {
                font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
                width:100%;
                border-collapse:collapse;
            }
            #table td, #table th
            {
                font-size:1em;
                border:1px solid #000000;
                padding:3px 7px 2px 7px;
            }
            #table th
            {
                font-size:1.1em;
                text-align:left;
                padding-top:5px;
                padding-bottom:4px;
                background-color:#0080FF;
                color:#ffffff;
                }
            #table tr.alt td 
                {
                color:#000000;
                background-color:#A9E2F3;
                }
        </style>
    </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>   
            <br/>

            <button onclick="location.href='viewTransactionHistory.php'">Back</button>
    </div>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

一种选择是使用服务器端代码从HTML生成PDF。 HTML2PDF是一个选项:http://sourceforge.net/projects/phphtml2pdf/

还有另一个名为Dompdf的工具:http://www.sitepoint.com/convert-html-to-pdf-with-dompdf/