FPDF skips first row - php

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();
}
}

Related

Labels created with PHP FPDF Library slide down page

I have created an FPDF PDF document that puts an image on label paper which has 9 rows of labels (PHP code below)
There is no space/gap between each row of labels, and the next row of labels start immediately after the previous row.
### THE ISSUE: ###
When the labels are printed, the image displayed on each label moves down slightly from the top of each label, causing the bottom row of labels (9th row) to be significantly different to the 1st couple of rows.
I need to add additional content to each label, and if this issue continues, some of this content is going to get cut off.
I don't understand why this is happening, and can't see anything obvious in the code. Can anyone here spot what I'm doing wrong?
My code.....
use Fpdf\Fpdf as FPDF;
$current_x_position = 0;
$current_y_position = 0;
$total_y_per_page = 9;
$total_x_per_page = 3;
$pdf = new FPDF();
$pdf->SetMargins(0,0);
$pdf->SetAutoPageBreak(false);
$pdf->AddPage();
for($qty = 1; $qty <= 10; $qty++) {
label($current_x_position, $current_y_position, $pdf);
$current_y_position++;
if($current_y_position == $total_y_per_page) {
$current_x_position++;
$current_y_position = 0;
if($current_x_position == $total_x_per_page) {
$current_x_position = 0;
$current_y_position = 0;
$pdf->AddPage('P');
}
}
}
$pdf->Output();
function label($current_x_position, $current_y_position, $pdf) {
$left_margin = 7;
$top_margin = 15;
$label_width = 66;
$label_height = 30;
$current_x_position = $left_margin + ($label_width * $current_x_position);
$current_y_position = $top_margin + ($label_height * $current_y_position);
$pdf->Image('image.png', $current_x_position, $current_y_position+=1, $label_width, 10, 'png');
return;
}
?>
I think the problem stems from how x is being handled. It should be distinct from y. Why? Because x needs to be reset after every 3rd label is printed. Since it is only tested and incremented after 9 rows are printed, it will surely cause slueing. (Maybe that's an old-timey expression, used to indicate a program causes a new line because it is already past the column in which you want to print). Suggest you need to test x after every label print, so it can be reset to 0 when it reaches total_x_per_page (which I think really means total_x_per_row). The x test needs to be independent of the y test; it needs to be before the y test because you the columns will be complete before the rows.
In pseudo-ish code:
print a label
if the row is complete reset x, otherwise increase x
if the page is complete, reset y and x.
My experience with Fpdf is nil. My experience with (fighting) labels goes back decades :)

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

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

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.

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