Trying to set up a table with MultiCell in FPDF - php

In PHP I'm trying to set up a 'table' but can't seem to set my cell height uniformly along a row:
And I've added a line break so you can see the issue more clearly:
I would like to get a clean table format where the row where "This is my long..." is all the same cell height. But the only function on the documents was GetStringWidth() so I don't see how I can apply that to the case here.
This is the desired output:
This is my code:
//Custom hard coded widths for the template
$width = array(12, 20, 30, 20, 15, 12, 20, 18, 25, 15, 10);
foreach ($row as $col) {
$y = $this->GetY();
$x = $this->GetX();
$this->MultiCell($width[$i], 3, $col, 1, 'C', FALSE);
$this->SetXY($x + $width[$i], $y);
$i++;
}
EDIT:
After doing some more research I found that this library won't give me what I need, it actually lacks majority of the things a basic library has so I've decided to use tcpdf as my library.

For such result, use this script: http://www.fpdf.org/en/script/script3.php

Related

FPDF after page break using SetAutoPageBreak multicell values are not inserted in the defined Y position

I am trying to display data using multiCell. And when the data in the page arrives to y=228 I want it to go to the next Page And be displayed in the position y=112.
As first Step I tried to only add 2 simple conditions :
when the data arrives to position y=228 create a new page
when the data goes to the next Page display the result at position =112
It worked. But if the current multiCell content is large it dosn't go to the next page untill it finishs writting all the multicell content , so I added the function SetAutoPageBreak So it inserts a page Break when y=228 .
Here where the problems start The code doesnt insert my data in the new page in the position I defined (y=112) it insert it in the start .I don't know how to fix this problem I hope I could find some help I will appreciate it.
Here is My code :
<?php
require ('../fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 12);
$y = $pdf->GetY();
$x = $pdf->GetX();
$width_cell = array(
10,
30,
50
);
$pdf->SetFillColor(193, 229, 252);
$pdf->SetY(112);
$pdf->Cell($width_cell[0], 10, 'ID', 1, 0, C, true);
$pdf->Cell($width_cell[1], 10, 'NAME', 1, 0, C, true);
$pdf->Cell($width_cell[2], 10, 'CLASS', 1, 1, C, true);
$pdf->SetFont('Arial', '', 10);
for ($i = 0;$i < 30; $i++) {
$pdf->Cell($width_cell[0], 10, $i, 1, 0, C, false);
$pdf->Cell($width_cell[1], 10, 'John Deo', 1, 0, C, false);
$pdf->Cell($width_cell[2], 10, 'Four', 1, 1, C, false);
$y = $pdf->GetY();
$pdf->Cell($width_cell[0], 10, $i, 1, 0, C, false);
$pdf->Cell($width_cell[1], 10, 'Y:' . $y, 1, 0, C, false);
// $pdf->Cell($width_cell[2],10,'Four',1, 1, C, false);
$pdf->MultiCell($width_cell[2], 10, 'four four four four four four four four four four four four four four four four four four four four four four ', 1, C, false);
// Uncomment this line to see what Happends when the Page Break is inserted
// $pdf->SetAutoPageBreak(auto,69);
$y = $pdf->GetY();
if ($y > 228 && $i != 29) {
$pdf->AddPage();
$pdf->SetY(112);
}
/*
if ($pdf->PageNo() != 1 && $y < 20){
$pdf->SetY(112);
}
*/
}
$pdf->Output();
?>
Extend fPDF and add a header function which positions Y where you want it every time a new page is started.
require ('fpdf.php');
class PDF extends FPDF {
function Header() {
$this->SetY(112);
}
} // end of the PDF class
$pdf = new pdf();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
$y=$pdf->GetY();
$x=$pdf->GetX();
$width_cell=array(10,30,50);
$pdf->SetFillColor(193,229,252);
$pdf->SetY(112);
The rest of your code goes here and you DO want to uncomment the AutoPageBreak line. You'll also want to change the AutoPageBreak line to read
$pdf->SetAutoPageBreak(1,69);
since the first argument is a Boolean to indicate whether or not it should be enabled.

How to highlight a cell data (formed using an array) based on some results in FPDF

I want to highlight specific cell based on results in FPDF. I am displaying cells using array.
$a = array(1, 2, 3, 4, 5);
foreach($a as $col)
$pdf->Cell(32,10,$col,1,0,'C');
Each of these array values are basically results. So for example
if result == 3
I want cell 3 that has 3 written in it can highlight in some manner(advice is welcomed) I thought about making a circle there or changing cell background/border colour or changing text colour.
I tried something like this but it does not work
if(in_array(3,$a ,TRUE)){
$pdf->SetFillColor(128, 0, 0);
$pdf->SetTextColor(255, 255, 255);
}
Can somebody give me hints only through FPDF and php please.
If you are saying that you only want to fill the array value that equals 3, this is how you could do it:
$pdf = new FPDF();
$a = array(1, 2, 3, 4, 5);
foreach($a as $col) {
if ($col == 3) {
$pdf->SetFillColor(128, 0, 0);
$pdf->SetTextColor(255, 255, 255);
} else {
$pdf->SetFillColor(255);
$pdf->SetTextColor(0);
}
$pdf->Cell(32,10,$col,1,0,'C');
}
The if (in_array(3, $a)) wouldn't work because by saying that you are setting the fill and text color for all if there is a 3.
Simplify the if guard, better yet remove it altogether and test the background color code
if(true){
$pdf->SetFillColor(128, 0, 0);
$pdf->SetTextColor(255, 255, 255);
}

FPDF align text LEFT, Center and Right

I've have three cells and i'am trying to align the text to Left, Center and Right.
function Footer()
{
$this->SetY( -15 );
$this->SetFont( 'Arial', '', 10 );
$this->Cell(0,10,'Left text',0,0,'L');
$this->Cell(0,10,'Center text:',0,0,'C');
$this->Cell( 0, 10, 'Right text', 0, 0, 'R' );
}
When I ouput my PDF file, center text get automatically aligned right.
This is how it looks:
Can somebody tell me what I'm doing wrong here and how I can fix this issue?
The new position after a Cell call will be set to the right of each cell if you set the ln-parameter of the Cell method to 0. You have to reset the x-coordinate before the last 2 Cell calls:
class Pdf extends FPDF {
...
function Footer()
{
$this->SetY( -15 );
$this->SetFont( 'Arial', '', 10 );
$this->Cell(0,10,'Left text',0,0,'L');
$this->SetX($this->lMargin);
$this->Cell(0,10,'Center text:',0,0,'C');
$this->SetX($this->lMargin);
$this->Cell( 0, 10, 'Right text', 0, 0, 'R' );
}
}
Though Jan Slabon's answer was really good I still had issues with the center not being exactly centered on my page, maybe we have different versions of the library and that's what accounts for the slight differences, for instance he uses lMargin and on some versions that's not available. Anyway, the way it worked for me is this:
$pdf = new tFPDF\PDF();
//it helps out to add margin to the document first
$pdf->setMargins(23, 44, 11.7);
$pdf->AddPage();
//this was a special font I used
$pdf->AddFont('FuturaMed','','AIGFutura-Medium.ttf',true);
$pdf->SetFont('FuturaMed','',16);
$nombre = "NAME OF PERSON";
$apellido = "LASTNAME OF PERSON";
$pos = 10;
//adding XY as well helped me, for some reaons without it again it wasn't entirely centered
$pdf->SetXY(0, 10);
//with SetX I use numbers instead of lMargin, and I also use half of the size I added as margin for the page when I did SetMargins
$pdf->SetX(11.5);
$pdf->Cell(0,$pos,$nombre,0,0,'C');
$pdf->SetX(11.5);
//$pdf->SetFont('FuturaMed','',12);
$pos = $pos + 10;
$pdf->Cell(0,$pos,$apellido,0,0,'C');
$pdf->Output('example.pdf', 'F');
Seems that there is a parameter for this, so ('R' for right-aligning, 'C' for centering):
$pdf->Cell(0, 10, "Some text", 0, true, 'R');
will right align text.
Also, notice that the first parameter ('width') is zero, so cell has 100% width.

i want to use pdf function in php multicell in one line parallel cell text left top align

into cell tag how can i use text in left top if the text block is large.
there is the main problem in Multicell i can't use twomulticell parallel .
$pdf->MultiCell(60, 6, "".$row['particular'], 1, 'L', FALSE);
$pdf->Cell(40,50,"".$row['quantity'],1,0,"l");
The MultiCell method has no $ln parameter like the Cell method (Just a side note: Internally a MultiCell creates the lines by several Cell calls). If you need to stay at the same line with a multicell you have to do this with your own logic. E.g.:
$y = $pdf->GetY();
$x = $pdf->GetX();
$width = 60;
$pdf->MultiCell($width, 6, 'particular', 1, 'L', FALSE);
$pdf->SetXY($x + $width, $y);
$pdf->Cell(40,50, 'quantity', 1, 0, "l");

Inserting an image with PHP and FPDF

I'm trying to insert an image but do not want to specify the x and y coordinates. Is that possible?
$pdf->Image($image1, 5, 70, 33.78);
I want to be able to specify the size (33.78) but not the x and y so that it moves based on the content.
$pdf->Write( 70, $reportTitle );
$pdf->Ln( 45 );
$pdf->SetFont( 'Arial', '', 12 );
$pdf->Write( 6, $reportSubtitle );
/**
Create product 1
**/
$pdf->Ln( 10 );
$pdf->SetFont( 'Arial', '', 12 );
$pdf->Write( 6, $prod1title );
$pdf->Ln( 30 );
$pdf->SetFont( 'Arial', '', 10 );
$pdf->Write( 5, $prod1sub );
$pdf->Ln( 30 );
$pdf->Image($image1, 5, 70, 33.78);
The above is the code I use. If $reportSubtitle is two or three lines, it pushes $prod1title and $$prod1sub down, and inevitably under the image that is fixed. Is there no way to have the image act like the product title and subtitle and be pushed down too while still declaring the size?
I figured it out, and it's actually pretty straight forward.
Set your variable:
$image1 = "img/products/image1.jpg";
Then ceate a cell, position it, then rather than setting where the image is, use the variable you created above with the following:
$this->Cell( 40, 40, $pdf->Image($image1, $pdf->GetX(), $pdf->GetY(), 33.78), 0, 0, 'L', false );
Now the cell will move up and down with content if other cells around it move.
Hope this helps others in the same boat.
You can use $pdf->GetX() and $pdf->GetY() to get current cooridnates and use them to insert image.
$pdf->Image($image1, 5, $pdf->GetY(), 33.78);
or even
$pdf->Image($image1, 5, null, 33.78);
(ALthough in first case you can add a number to create a bit of a space)
$pdf->Image($image1, 5, $pdf->GetY() + 5, 33.78);
$image="img_name.jpg";
$pdf =new FPDF();
$pdf-> AddPage();
$pdf-> SetFont("Arial","B",10);
$pdf-> Image('profileimage/'.$image,100,15,35,35);
Please note that you should not use any png when you are testing this , first work with jpg .
$myImage = "images/logos/mylogo.jpg"; // this is where you get your Image
$pdf->Image($myImage, 5, $pdf->GetY(), 33.78);
// Image URL
$url = 'img/img.png';
// Place the image in the pdf document
$pdf->Cell(30, 30, $pdf => Image($url, 5, $pdf => GetY(), 93.78), 0, 0, 'L', false);
The 93.78 is the size of the image.
5 is the position from the left side.
You can't treat a PDF like an HTML document. Images can't "float" within a document and have things flow around them, or flow with surrounding text. FPDF allows you to embed html in a text block, but only because it parses the tags and replaces <i> and <b> and so on with Postscript equivalent commands. It's not smart enough to dynamically place an image.
In other words, you have to specify coordinates (and if you don't, the current location's coordinates will be used anyways).

Categories