Export HTML table to PDF
本问题已经有最佳答案,请猛点这里访问。
我有一个HTML表,显示了用户帐户的交易历史记录。如何将此表导出为PDF?完全相同的数据和相同的表样式(CSS)?我是否需要使用第三方进行PDF?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | <?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> Transaction History <?php echo $date ."" .""; $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']; ?> RP INTERNET BANKING ACCOUNT <?php echo $acc_num ?> <?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">Total: </td> <td align="right"> <?php if ($total_debit != 0 ) { echo 'S$' . ' ' . $total_debit; } ?> </td> <td align="right"> <?php if ($total_credit != 0){ echo 'S$' . ' ' . $total_credit; } ?> </td> </tr> </tbody> </table> </center> <br/> <button onclick="location.href='viewTransactionHistory.php'">Back</button> </body> </html> |
一种选择是使用服务器端代码从HTML生成PDF。HTML2PDF是其中一个选项:http://sourceforge.net/projects/phphphtml2pdf/
还有另一个叫做dompdf的工具:http://www.sitepoint.com/convert-html-to-pdf-with-dompdf/