我正在处理mysql数据转换为pdf的问题,我需要以表格形式打印出一个列的数据,每个单元格都有这个列的结果。我想要实现以下格式:格式一和格式二。这是我的输出结果,不是我想要的:。我尝试过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
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();
}