3
我工作的MySQL數據轉換爲PDF &我只打印一個表格格式,這意味着每個單元格的列數據有一列結果如何在TCPDF或FPDF中正確格式化?
我想達到以下格式
這是我的輸出這是不期望:
我都嘗試FPDF和TCPDF,但我不能夠在轉換結果中所需的格式。
1.FPDF
require_once("dbcontroller.php");
$db_handle = new DBController();
$result = $db_handle->runQuery("SELECT DISTINCT SUBSTRING(`V_REGISTRATION_NO` , 1, 4) AS V_SUB_REGISTRATION_NO FROM `tbl_vehicle` WHERE V_STATUS_CAUGHT = 'false' AND V_BANK_ID='1' AND V_BRANCH_ID='1' ORDER BY `V_REGISTRATION_NO` ASC ");
$header = $db_handle->runQuery("SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='poniyaag_new'
AND `TABLE_NAME`='tbl_vehicle'
AND COLUMN_NAME='V_REGISTRATION_NO'
");
require('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
foreach($header as $heading) {
foreach($heading as $column_heading)
$pdf->Cell(60,12,$column_heading,1);
}
foreach($result as $row) {
$pdf->SetFont('Arial','',8);
$pdf->Ln();
foreach($row as $column)
$pdf->Cell(65,12,$column,1);
}
}
$pdf->Output();
2.TCPDF
在 TCPDFob_start();
require_once('tcpdf/examples/lang/eng.php');
require_once('tcpdf/tcpdf.php');
require_once("dbcontroller.php");
error_reporting(E_ALL) ; ini_set('display_errors', '1');
$con=mysql_connect("localhost","root","");
if(!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("poniyaag_new");
$result = mysql_query("SELECT DISTINCT SUBSTRING(`V_REGISTRATION_NO` , 1, 4) AS V_SUB_REGISTRATION_NO FROM `tbl_vehicle` WHERE V_STATUS_CAUGHT = 'false' AND V_BANK_ID='1' AND V_BRANCH_ID='1' ORDER BY `V_REGISTRATION_NO` ASC");
if(($result)){
$html = '';
$html .= "<table width='width: 638px;' border='1px'><tr>";
if(mysql_num_rows($result) > 0){
$i = 0;
while($i < mysql_num_fields($result)){
$html .= "<th>". mysql_field_name($result, $i) . "</th>";
$i++;
}
$html .= "</tr>";
$html .= "<tr>";
while($rows = mysql_fetch_array($result, MYSQL_ASSOC)){
foreach ($rows as $data){
$html .= "<td align='center'>". $data . "</td>";
}
// $html.="</tr>";
}
}else{
$html .= "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>";
}
$html .= "</table>";
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// Set various pdf options
$pdf->SetAuthor('John Doe');
// etc.
// Now output the html
$pdf->AddPage();
$pdf->writeHTML($html, true, 0);
// Output the PDF to the browser
ob_end_clean();
$pdf->Output('somefile.pdf', 'D'); // the second option D forces the browser to download the PDF. Passing I will tell the browser to show it inline.
}else{
echo "Error in running query :". mysql_error();
}
向我們展示您的輸出文件。 – Yash
@Yash我的輸出圖像被添加 – SSangeet
http://www.fpdf.org/en/tutorial/tuto5.htm你可以試試這個@Ssangeet – Yash