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.
Related
I'm trying to write text to a PDF and there seems to be a weird margin on the top of my page.
This is my following code:
require_once('fpdf.php');
require_once('fpdi/fpdi.php');
//Start the FPDI
$pdf = new FPDI('P', 'pt');
//Set the source PDF file
$source_file = $pdf->setSourceFile("template.pdf");
//Import the first page of the file
$tppl = $pdf->importPage(1);
$pdf->AddPage();
//get size of pdf page
$size = $pdf->getTemplateSize($tppl);
$pdf->useTemplate($tppl, null, null, $size['w'], $size['h'], true);
$pdf->SetMargins(0, 0, 0);
$pdf->SetTextColor(0, 0, 0);
When I use a font-size pt 12, and write text I get this:
$pdf->SetFont('Arial', '', 12);
$pdf->SetXY(0, 0);
$pdf->Write(0, "Hi");
When I do $pdf->SetXY(0, 7.5) I get this
The above looks like I can easily add 7.5 points to the Y and be fine.
However, if I changed the font-size, the distance between the top and the text grows a little greater.
$pdf->SetFont('Arial', '', 8);
Could anyone help me figure out how to neutralize this to at least make it so if I set my XY to a number, it will put it on the some location regardless of the font-size? I've tried different pdf's and it works all the same.
EDIT:
I did $pdf->GetY() and I get 28.35
You simply define a line height of zero. Because of this the text is "centered" vertically around 0.
A common line height is:
$pdf->Write($pdf->FontSize * 1.2, "Hi");
I solved this by instead of doing Write() I used Cell().
I think the main issue was not having a solid width and height. All I know is it works perfectly now so anyone encountering the same problems should try this.
$pdf->Cell(WIDTH,HEIGHT,TEXT);
I also did the following, not sure if this helped or not but I have it in my script.
$pdf->SetMargins(0, 0);
$pdf->cMargin = 0;
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');
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.
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();
}
}
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..!!