0

enter image description hereFPDF table

<?php
require('fpdf.php');
include("dbConfig.php");
$CUST_ID ="3000/001";

$result = mysql_query("SELECT * FROM com_profile WHERE CUST_ID = '$CUST_ID'");
while ($row = mysql_fetch_array($result)) {
    $CUST_NAME = $row["CUST_NAME"];
    $CUST_NAME2 = $row["CUST_NAME2"];
    $CUST_TEL = $row["CUST_TEL"];
    $CUST_FAX = $row["CUST_FAX"];
    $CUST_EMAIL = $row["CUST_EMAIL"];
}
$CUST_NAME = htmlspecialchars_decode($CUST_NAME) ;
$CUST_NAME2 = htmlspecialchars_decode($CUST_NAME2);
$CUST_TEL = htmlspecialchars_decode($CUST_TEL);
$CUST_FAX = htmlspecialchars_decode($CUST_FAX);
$CUST_EMAIL = htmlspecialchars_decode($CUST_EMAIL);
/**
* header and footer for pdf
*/
class PDF extends FPDF
{

}

// Instantiation of inherited class
$pdf = new PDF('L', 'mm', 'A4');
$pdf -> AliasNbPages();
$pdf -> SetTitle("Point History");
$pdf -> AddPage();
$pdf -> SetFont('Times', 'U', 16);
$pdf -> Cell(5);
$pdf -> Cell(30,10,'Point Transactions:');
$pdf ->SetFont('Times', 'B', 12);
$pdf -> Ln(5);
$pdf -> Cell(10);
$pdf -> Cell(35,10, 'Company Code :');
$pdf ->SetFont('Times', '', 12);
$pdf -> Cell(30, 10, $CUST_ID);
$pdf -> Ln(5);
$pdf -> Cell(10);
$pdf ->SetFont('Times', 'B', 12);
$pdf -> Cell(35,10, 'Company Name :');
$pdf ->SetFont('Times', '', 12);
$pdf -> Cell(30, 10, $CUST_NAME);
$pdf ->Ln(5);
$pdf ->Cell(45);
$pdf -> Cell(30, 10, $CUST_NAME2);
$pdf -> Ln(5);
$pdf -> Cell(10);
$pdf ->SetFont('Times', 'B', 12);
$pdf -> Cell(35,10, 'Tel :');
$pdf ->SetFont('Times', '', 12);
$pdf -> Cell(30, 10, $CUST_TEL);
$pdf -> Ln(5);
$pdf -> Cell(10);
$pdf ->SetFont('Times', 'B', 12);
$pdf -> Cell(35,10, 'Fax :');
$pdf ->SetFont('Times', '', 12);
$pdf -> Cell(30, 10, $CUST_FAX);
$pdf -> Ln(5);
$pdf -> Cell(10);
$pdf -> SetFont('Times', 'B', 12);
$pdf -> Cell(35,10, 'Email :');
$pdf ->SetFont('Times', '', 12);
$pdf -> Cell(30, 10, $CUST_EMAIL);

$pdf -> Ln(15);
$pdf -> Cell(5);
$pdf -> SetFont('Times', 'B', 10);
$pdf -> SetDrawColor(50,50,100);
$pdf -> Cell(155,8,'SERVICE DESCRIPTION', 1,0,'',false);
$pdf -> Cell(20,8,'POINT(-)', 1,0,'',false);
$pdf -> Cell(20,8,'POINT(+)', 1,0,'',false);
$pdf -> Cell(20,8,'BALANCE', 1,0,'',false);
$pdf -> Cell(25,8,'DATE', 1,0,'',false);
$pdf -> Cell(25,8,'SERV. BY', 1,1,'',false);

$pdf -> SetFont('Times', '', 10);

// query to retrieve data from database
$result = mysql_query("SELECT * FROM point_histories WHERE CUST_ID = '$CUST_ID' ORDER BY CON_ID ASC");
$Y = 63;

while ($row = mysql_fetch_array($result)) {
    $pdf -> SetXY(15, $Y);
    $pdf -> MultiCell(155,8,$row['SERVICE_DESP'], 1,"L");

    $H = $pdf -> GetY();
    $height = $H - $Y;

    $pdf -> SetXY(170, $Y);
    $pdf -> Cell(20,$height,$row['P_DEDUCTED'], 1,"L");
    $pdf -> SetXY(190, $Y);
    $pdf -> Cell(20,$height,$row['P_ADDED'], 1,"L");
    $pdf -> SetXY(210, $Y);
    $pdf -> Cell(20,$height,$row['P_BALANCES'], 1,"L");
    $pdf -> SetXY(230, $Y);
    $pdf -> Cell(25,$height,$row['MODIFIED_DATE'], 1,"L");
    $pdf -> SetXY(255, $Y);
    $pdf -> Cell(25,$height,$row['TC_SUPPORT'], 1, "L");
    $Y = $H;
}

$pdf -> Output();
?>

I trying to generate fpdf table that loads data from MySQL table, when the content can fit inside one page, table is fine. But when the content need to extends to second page, the table column disoriented just like shown in the attached pic. I know that it has something to do with the y-axis but I don't know how to fix this, can someone help?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • It should like the table shown in the pic 1. The columns of first row had is attached to second row. – Kimkim900109 Aug 29 '17 at 02:42
  • If you're writing new code, **_please_ don't use the `mysql_*` functions**. They are old and broken, were deprecated in PHP 5.5 (which is so old it no longer even receives security updates), and completely removed in PHP 7. Use [`PDO`](https://secure.php.net/manual/en/book.pdo.php) or [`mysqli_*`](https://secure.php.net/manual/en/book.mysqli.php) with _prepared statements_ and _parameter binding_ instead. See http://stackoverflow.com/q/12859942/354577 for details. – ChrisGPT was on strike Aug 29 '17 at 02:48
  • ok,, will make changes to the code soon. Thanks . – Kimkim900109 Aug 29 '17 at 08:11

2 Answers2

0

You have a problem with AutoPageBreak because of your use of MultiCell so turn it off with $pdf -> SetAutoPageBreak( false ); and make the call manually after when near the bottom of the page and AddPage when at the bottom or if the line is long enough that it would create a cell larger than the current page. Also, since you have make your on class extension of FPDF please make a function or two instead of repeating it all.

Tyler Christian
  • 520
  • 7
  • 14
0

It's easier to use EasyTable. Pagebreak won't be problem.

Ex:

require('fpdf/exfpdf.php');
require('fpdf/easyTable.php');
...

$tableB=new easyTable($pdf, '6', 'align:L; width:100%; split-row:true;  border:0; border-color:#000000; bgcolor:#ffffff; font-color:#000000; paddingY:0;');
    $tableB->easyCell("Data1", 'border: LB;  align: L; valign:M');
    $tableB->easyCell("Data2", 'border: LB;  align: L; valign:M');
    $tableB->easyCell("Data3", 'border: LB;  align: L; valign:M');
    $tableB->easyCell("Data4", 'border: LB;  align: L; valign:M');
    $tableB->easyCell("Data5", 'border: LB;  align: L; valign:M');
    $tableB->easyCell("Data6", 'border: LB;  align: L; valign:M');
    $tableB->printRow();
$tableB->endTable(2);