PHP | FPDF : Space between 5th and 6th cell - php

I am troubling in generating this report..
I am aiming something like this
But I only have this
I want the 5th and 6th cell to have a space between them..
and here is my code so far:
$pdf = new PDF('L', 'mm', array(215.9, 330.2));
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial','',11);
$pdf->Cell(132);
$pdf->Cell(45,15,'Teacher'."'s ".'Table',1,'','C');
$pdf->Ln(25);
while($row = mysql_fetch_array($sqlresult)){
if($pdf->GetX() < 290){
$pdf->SetFont('Arial','',10);
$pdf->Cell(30,15,$row['stud_fname'].", ".$row['stud_lname'],1,'','C');
}else{
$pdf->Ln(20);
}
}
$pdf->Output();
any idea would be appreciated, thanks in advance :)

Count the cells and create an 'invisible' cell between the 5th and the 6th.
EDIT: I think your current code is wrong, when $pdf->GetX() > 290 you are creating a new line BUT are also skipping the mysql result (you are not writting it). I have updated my answer with what I think is right.
$thisCell=1;
while($row = mysql_fetch_array($sqlresult)){
if($pdf->GetX() >= 290){
$pdf->Ln(20);
$thisCell=1;
}
$pdf->SetFont('Arial','',10);
$pdf->Cell(30,15,$row['stud_fname'].", ".$row['stud_lname'],1,'','C');
if ($thisCell==5)
$pdf->Cell(10,15,'',0); //cell without borders
$thisCell++;
}

Related

Loops in FPDF library

I am new to using FPDF and cant get my head around it. Here is the simple case:
$pdf = new FPDF();
$pdf->AddPage();
for($i=0; $i< 10; $i++){
$pdf->SetFont('Arial', "B", 12);
$pdf->Cell(40, 10, "asd");
}
$pdf->Output();
The browser says it failed to load PDF document.
whats wrong?
Well the code itself works for me. As stupid as it might sound, did you include the fpdf.php?
require('your/dircetory/fpdf.php');

FPDF in CodeIgniter show only a blank sheet

I have two features which uses FPDF in the application I am developing. The other one prints things correctly, while the other outputs only a blank sheet and I really dont know where's the error in here. I already tried to var_dump to know if values are fetch correctly from the database and everything is good except for this printing the pdf part. What should I do?
Here is the code snippet for the one that only outputs a blank sheet.
function generate(){
$sample= $this->model_a->get_a();
$j = 10;
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetMargins(0.5, 0.5, 0.5);
$pdf->Cell(40,4,'TITLE','',0,'C',0);
$pdf->setXY(12.5,$j+=4);
$pdf->SetFont('times','B',10);
$pdf->Cell(42,4,'COL1','TLRB',0,'C',0);
$pdf->Cell(100,4,'NAME','TLRB',0,'C',0);
$pdf->Cell(42,4,'NAME2','TLRB',0,'C',0);
foreach($sample as $s){
$x= $s['id'];
$y= "{$s['f_name']}, {$s['l_name']} {$s['mi']}.";
$z= "{$s['f_name1']}, {$s['l_name1']} {$student['mi1']}.";
$this->addRow($pdf,$x,$y,$z, $j);
}
$pdf->Output();
}
function addRow($pdf,$x,$y,$z,&$j){
$pdf->setXY(12.5,$j+=4);
$pdf->SetFont('times','',10);
$pdf->Cell(42,4,$x,'TLRB',0,'C',0);
$pdf->Cell(100,4,$y,'TLRB',0,'C',0);
$pdf->Cell(42,4,$z,'TLRB',0,'C',0);
}
I think that the function addRow have to return the pdf object. Alternatively you can create the new FPDF in a variable class ($this->pdf) and change all the other $pdf.

FPDF SetX and SetY call order bug?

It seems like the order of SetX() and SetY() does matter. As you can see, the second cell-box in the example is located at following coordinates: X:10.00125/Y:80. Actually it should be at x=80. Setting Y-coordinate first fixes the problem. Is it a bug? PHP version used is 5.3.28.
<?php
require('./fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->SetY(50);
$pdf->SetX(80);
$pdf->Cell(0,5,'Coordinates: X:'.$pdf->GetX().'/Y:'.$pdf->GetY(), 1);
$pdf->SetX(80);
$pdf->SetY(80);
$pdf->Cell(0,5,'Coordinates: X:'.$pdf->GetX().'/Y:'.$pdf->GetY(), 1);
$pdf->Output();
?>
This is obvious. Look at the source or the manual:
function SetY($y)
{
// Set y position and reset x
$this->x = $this->lMargin;
if($y>=0)
$this->y = $y;
else
$this->y = $this->h+$y;
}
So this seems to be no bug. x is reset to the left-margin, what you already noticed. You could use SetXY($x, $y) instead.
I think they wanted to have SetY to be used for placing the next paragraph, so its always aligned to the left side.

FPDF skips first row

I wrote a simple FPDF code but I ran into a problem.
For some reason, the for loop skips the first row ( cell 1, cell 2, cell 3 ).
Code:
<?php
require('temp/fpdf.php');
class PDF extends FPDF
{
function Header(){
$this->SetY(0);
$this->SetFont('Arial','I',8);
$this->Cell(0,5,'Page '.$this->PageNo(),0,0,'C');
}
function Footer(){
$this->SetY(-5);
$this->SetFont('Arial','I',8);
$this->Cell(0,5,'Page '.$this->PageNo(),0,0,'C');
}
}
$pdf = new PDF();
$pdf->SetMargins(0,0,0);
$pdf->AddPage();
$pdf->SetFont('Times','',12);
for($i=0;$i<=24;$i++){
$pdf->Cell(70,30,'Printing line number '.$i,0,0,'C');
if(($i%3==0)&&($i!=0)){
$pdf->Ln();
}
}
$pdf->Output();
?>
I'm staring at the code for hours but I can't find the answer so any help is appreciated.
Your first set of printing is floating over to the right side. As a quick-fix try adding to add a line break and clear up unwanted floats in the PDF definition.
Add
$pdf->Ln();
after
$pdf->AddPage();
I see two possibilities that you might want to check. The first is that the margins are all zero, but in order for a PDF to print or save correctly, they need to be 1" on top/bottom and .5" on the sides. Also you have to consider that the first iteration of your loop $i will equal 0, and your first cell will print out 0 and not 1. Let me know if that doesn't pan out, and I can help more. I'm actually in the middle of a huge FPDF project and have found many nuances.
I just noticed this:
if(($i%3==0)&&($i!=0)){
$pdf->Ln();
}
Use this http://calculator.sdsu.edu/calculator.php to calculate the Modulus using your $i values and you'll see 0%3=0, 1%3=4, 2%3=5. That will skip your first line.
I personally would've written it like this:
$i = 1;
while($i <=25)
{
$pdf->Cell(70,30,'Printing line number '.$i,0,0,'C');
if(($i % 3) == 0)
{
$pdf->Ln();
}
}

PHP - FPDI - Can't Write to more than One Page

The problem is, I need to write data to several copies of the same imported pdf file, and save it as one pdf. I can write data to one page just fine, but when I try to write to more than one, or even continue text (using SetAutoPageBreak()), it simply stops writing once it hits the next page. Although, if I add an arbitrary loop to write more data, the resulting pdf's number of pages is increased to accommodate the added data, but the pages beyond the first are still blank. I have simplified what I'm trying to do into a smaller example to illustrate the issue:
public function actionSample() {
$pdf = new FPDI();
$pdf->AcceptPageBreak();
$pdf->SetAutoPageBreak(true, 30);
$pagecount = $pdf->setSourceFile('images/sample.pdf');
for ($i = 1; $i <= $pagecount; $i++) {
$pdf->AddPage();
$tplidx = $pdf->ImportPage($i);
$pdf->useTemplate($tplidx, 10, 10, 200);
$s = $pdf->getTemplatesize($tplidx);
$pdf->SetTextColor(32,32,32);
$pdf->SetFontSize(10);
$pdf->SetXY($pdf->getX(), $pdf->getY()+10);
$pdf->Write(2, 'This is not!');
}
$pdf->Output('Sample.pdf', 'D');
}
The sample document has 3 blank pages initially. (I did this to make it easier to see what was being written)
$pdf->AddPage();
you have to just put this piece of code inside for loop near closing braces.
All the best..!!

Categories