如何在TCPDF或FPDF中正确格式化?

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

请展示给我们您的输出文件。 - Yash
@Yash 我的输出图像已经添加。 - SSangeet
请尝试访问此链接:http://www.fpdf.org/en/tutorial/tuto5.htm。@Ssangeet - Yash
@Yash 这是不同的输出格式,我需要只打印一列结果并在所有其他单元格中重复它。请检查我在问题中添加的格式1和格式2。 - SSangeet
这解决了你的问题吗? - Peyman Mohamadpour
@Trix 我等会儿告诉你,其实在你发帖之前我已经解决了,但我一定会尝试你建议的方法。 - SSangeet
1个回答

0

TCPDF 中,您应该更改代码片段中的此部分:

$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++;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接