this thing made me crazy man. let make it simple. i try to generate a pdf file which will contains student names. currently, all students divided into several programmes. CS225, CS226 and CS220. total number of students are 57. each page will has 25 student names and also a page header. my problem is, when i run the script, each row that contains student name is not at the right place. it is like this.
_____________________________
|____________________________| ----> header
|____________________________| ----> 1st student
|____________________________| ----> 2nd student
_____________________________
|____________________________| ----> 3rd student
_____________________________
|____________________________| -----> 4th student
it is not what i wanted. there is a big space between rows. suppose each row is comes after previous row.
[SOLVED] i need help on pdf generation using fpdf class.
Moderator: General Moderators
sorry, my desktop hung when i tried to post the php code. this is the code.
Code: Select all
<?php
//PDF USING MULTIPLE PAGES
//FILE CREATED BY: Carlos José Vásquez Sáez
//YOU CAN CONTACT ME: carlos@magallaneslibre.com
//FROM PUNTA ARENAS, MAGALLANES
//INOVO GROUP - http://www.inovo.cl
define('FPDF_FONTPATH','fpdf/font/');
require('fpdf/fpdf.php');
//Connect to your database
include("../connection/openconn.php");
//Create new pdf file
$pdf=new FPDF();
//Open file
$pdf->Open();
//Disable automatic page break
$pdf->SetAutoPageBreak(false);
//Add first page
$pdf->AddPage();
//set initial y axis position per page
$y_axis_initial = 20;
//print column titles for the actual page
$pdf->SetFillColor(232,232,232);
$pdf->SetFont('Courier','B',12);
$pdf->SetY($y_axis_initial);
$pdf->SetX(15);
$pdf->Cell(20,6,'NO',1,0,'L',1);
$pdf->Cell(80,6,'NAME',1,0,'L',1);
$pdf->Cell(30,6,'GROUP',1,0,'L',1);
$pdf->Cell(30,6,'PROGRAM',1,0,'L',1);
//Select the Products you want to show in your PDF file
$result = mysql_query("SELECT name, kelas, program FROM profile") or die(mysql_error());
//initialize counter
$i = 0;
//Set maximum rows per page
$max = 25;
$y_axis = 26;
$y_axis = $y_axis + $add;
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$kira = $kira + 1;
$name = $row["name"];
$group = $row["kelas"];
$program = $row["program"];
$pdf->SetY($y_axis);
$pdf->SetX(15);
$pdf->Cell(20,6,$kira,1,0,'L',1);
$pdf->Cell(80,6,$name,1,0,'L',1);
$pdf->Cell(30,6,$group,1,0,'L',1);
$pdf->Cell(30,6,$program,1,0,'L',1);
$add = $add + 6;
$y_axis = $y_axis + $add;
$i = $i + 1;
if ($i == $max)
{
$pdf->AddPage();
//print column titles for the current page
$pdf->SetY($y_axis_initial);
$pdf->SetX(15);
$pdf->Cell(20,6,'NO',1,0,'L',1);
$pdf->Cell(80,6,'NAME',1,0,'L',1);
$pdf->Cell(30,6,'GROUP',1,0,'L',1);
$pdf->Cell(30,6,'PROGRAM',1,0,'L',1);
//Go to next row
$y_axis = $y_axis + $row_height;
//Set $i variable to 0 (first row)
$i = 0;
}
}
mysql_close();
//Create file
$pdf->Output();
?>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
yep, as I guessed, you are cumulatively adding more space with:you don't even need $add it would seem. Simply adding 6 to $y_axis should do it:
Code: Select all
$add = $add + 6;
$y_axis = $y_axis + $add;Code: Select all
$y_axis += 6;dude! i solved it! after watched NUMA NUMA DANCE video clip. th correct code. i've been working with this code for 2 weeks!
Code: Select all
<?php
//PDF USING MULTIPLE PAGES
//FILE CREATED BY: Carlos José Vásquez Sáez
//YOU CAN CONTACT ME: carlos@magallaneslibre.com
//FROM PUNTA ARENAS, MAGALLANES
//INOVO GROUP - http://www.inovo.cl
define('FPDF_FONTPATH','fpdf/font/');
require('fpdf/fpdf.php');
//Connect to your database
include("../connection/openconn.php");
//Create new pdf file
$pdf=new FPDF();
//Open file
$pdf->Open();
//Disable automatic page break
$pdf->SetAutoPageBreak(false);
//Add first page
$pdf->AddPage();
//set initial y axis position per page
$y_axis_initial = 20;
//print column titles for the actual page
$pdf->SetFillColor(232,232,232);
$pdf->SetFont('Courier','B',12);
$pdf->SetY($y_axis_initial);
$pdf->SetX(15);
$pdf->Cell(20,6,'NO',1,0,'L',1);
$pdf->Cell(80,6,'NAME',1,0,'L',1);
$pdf->Cell(30,6,'GROUP',1,0,'L',1);
$pdf->Cell(30,6,'PROGRAM',1,0,'L',1);
//Select the Products you want to show in your PDF file
$result = mysql_query("SELECT name, kelas, program FROM profile") or die(mysql_error());
//initialize counter
$i = 0;
//Set maximum rows per page
$max = 25;
//Set location for 1st row of data
$y_axis = 26;
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
if ($i == $max)
{
$pdf->AddPage();
//print column titles for the current page
$pdf->SetY($y_axis_initial);
$pdf->SetX(15);
$pdf->Cell(20,6,'NO',1,0,'L',1);
$pdf->Cell(80,6,'NAME',1,0,'L',1);
$pdf->Cell(30,6,'GROUP',1,0,'L',1);
$pdf->Cell(30,6,'PROGRAM',1,0,'L',1);
//Go to next row
$y_axis = 26;
$row_height = 0;
//Set $i variable to 0 (first row)
$i = 0;
}
$kira = $kira + 1;
$name = $row["name"];
$group = $row["kelas"];
$program = $row["program"];
$y_axis = $y_axis + $row_height;
$pdf->SetY($y_axis);
$pdf->SetX(15);
$pdf->Cell(20,6,$kira,1,0,'L',1);
$pdf->Cell(80,6,$name,1,0,'L',1);
$pdf->Cell(30,6,$group,1,0,'L',1);
$pdf->Cell(30,6,$program,1,0,'L',1);
$row_height = 0; //add
$row_height = $row_height + 6;
$i = $i + 1;
}
mysql_close();
//Create file
$pdf->Output();
?>