2016-02-22 52 views
3

我工作的MySQL數據轉換爲PDF &我只打印一個表格格式,這意味着每個單元格的列數據有一列結果如何在TCPDF或FPDF中正確格式化?

我想達到以下格式

format one

format two

這是我的輸出這是不期望:

My Output

我都嘗試FPDFTCPDF,但我不能夠在轉換結果中所需的格式。

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

TCPDF
ob_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(); 
} 
+0

向我們展示您的輸出文件。 – Yash

+0

@Yash我的輸出圖像被添加 – SSangeet

+0

http://www.fpdf.org/en/tutorial/tuto5.htm你可以試試這個@Ssangeet – Yash

回答

0

你應該改變你的代碼段的這部分:

$html .= "<tr>"; 
while($rows = mysql_fetch_array($result, MYSQL_ASSOC)){ 
    foreach ($rows as $data){ 
     $html .= "<td align='center'>". $data . "</td>"; 
    } 
    // $html.="</tr>"; 
} 

到這一點:

$i = 0; 
while($rows = mysql_fetch_array($result, MYSQL_ASSOC)){ 
    foreach ($rows as $data){ 
     if(0 == $i % 5) 
      $html .= "<tr><td align='center'>". $data . "</td>"; 
     elseif(4 == $i % 5) 
      $html .= "<td align='center'>". $data . "</td></tr>"; 
     else 
      $html .= "<td align='center'>". $data . "</td>"; 
     $i++; 
    } 
}