fpdf multicell is not generating same height - php

I wrote below code , it working fine but multicell row heights are not working properly.I wrote below code , it working fine but multicell row heights are not working properly.I wrote below code , it working fine but multicell row heights are not working properly.
$x=$pdf->GetY();
$pdf->SetY($x+1);
include_once("config.php");
$result = mysqli_query($mysqli, "SELECT * FROM prd"); // using mysqli_query instead
$i = 1;
while($res = mysqli_fetch_array($result))
{
$current_y = $pdf->GetY();
$current_x = $pdf->GetX();
$pdf->MultiCell(30, 5, $i, 1, 'L');
$end_y = $pdf->GetY();
$prdid = $res[0];
$empid = $res[1];
$specification = $res[2];
$prn = $res[3];
$current_x = $current_x + 30;
$pdf->SetXY($current_x, $current_y);
$pdf->MultiCell(30, 5, $empid, 1, 'L');
$end_y = ($pdf->GetY() > $end_y)?$pdf->GetY() : $end_y;
$current_x = $current_x + 30;
$pdf->SetXY($current_x, $current_y);
$pdf->MultiCell(30, 5, $specification, 1, 'L');
$end_y = ($pdf->GetY() > $end_y)?$pdf->GetY() : $end_y;
$current_x = $current_x + 30;
$pdf->SetXY($current_x, $current_y);
$pdf->MultiCell(30, 5, $prn, 1, 'L');
$end_y = ($pdf->GetY() > $end_y)?$pdf->GetY() : $end_y;
$i++;
$pdf->SetY($end_y);
}
$pdf->Output();
?>
My Result :
How to adjust row height automatically ?

So a multi cell is essentially dynamic is height. The height you put into the function is a "row" height. So what happens is that fpdf goes to write the multi cell and let's say we defined the height to be 5, it'll create a "cell" of height 5 and start writing. Then it hits the hard stop at the width and goes "i have to create a new row", which it then adds a new "cell" of height 5 directly below the top "cell". This repeats until all text is written out. Obviously this is great for dynamic content, but has its own challenges that you have hit.
The path I normally take is to record the start point, write out the multi cell first, and then record the stopping point. You can then go back and write the other cells to make for better alignment. For this GetX, GetY, SetX, SetY will be your friends. You can set heights and such dynamically with simple math.
The "lazy" option is to re-do the layout to allow for the document to scale, that being to take your way too long multi cell and put it UNDER the row so you'll have:
|1 |46 |PR2.....|
|really long text
that will scale
down here|
|2 |........
Hope that helps get you moving forward!

Related

fcpdf give height to cell as per text

I am using FPDF and making table in it using FancyTable example from here http://www.fpdf.org/. Following, I am showing one glimpse of code.
$start_x = $this->GetX(); //initial x (start of column position)
$current_y = $this->GetY();
$current_x = $this->GetX();
$cell_height = 7;
$this->MultiCell($width,$cell_height,$data,0,'L',$fill);
$current_x+=$width; //calculate position for next cell
$this->SetXY($current_x, $current_y);
Output of this is following, You can see one cell data is going outside cell. I have highlighted one cell data that is going outside from cell.
If I increase the height of cell then it seems it increase the line height and not cell height. Following is sample code.
$cell_height = 25;
$this->MultiCell($width,$cell_height,$data,0,'L',$fill);
and output looks like following.

Trying to set up a table with MultiCell in FPDF

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

Positioning text over pdf file in PHP

I'm developing an e-Certificate web page.
I've managed loading the certificate template and writing on top of it (Attendee Name, Event Title and Event Date).
But in positioning those three pieces of information, I couldn't position them at the center no matter how long they are. they always start from x point.
Check my work result:
Code:
<?php
require_once('fpdf.php');
require_once('fpdi.php');
$pdf =& new FPDI();
$pdf->addPage('L');
$pagecount = $pdf->setSourceFile('Certificate.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx);
$pdf->SetFont('Times','I',18);
$pdf->SetTextColor(0,0,0);
$pdf->SetXY(120, 62);
$pdf->Write(0, "Attendee Name");
$pdf->SetXY(120, 102);
$pdf->Write(0, "Event Name");
$pdf->SetXY(120, 140);
$pdf->Write(0, "Event Date");
$pdf->Output();
?>
Is there away to center the text no matter how long is it?
meaning without fixing the x point. however, the y point result is very good.
So 2 ways I've done this before, the most likely easiest way:
Since you only have 1 entry on the row (Attendee Name or Event Name, etc)
Start a cell at the left most point, and have the cell run the full width of the page, then specify the center option:
$pdf->SetXY(0,62);
//0 extends full width, $height needs to be set to cell height.
$pdf->Cell(0, $height, "Attendee Name", 0, 0, 'C');
The other option is to calculate the center, and set X accordingly:
//how wide is the page?
$midPtX = $pdf->GetPageWidth() / 2;
//now we need to know how long the write string is
$attendeeNameWidth = $pdf->GetStringWidth($attendeeName);
//now we need to divide that by two to calculate the shift
$shiftLeft = $attendeeNameWidth / 2;
//now calculate our new X value
$x = $midPtX - $shiftLeft;
//now apply your shift for the answer
$pdf->setXY($x, 62);
$pdf->Write(0, "Attendee Name");
You can then repeat the above for each of the other elements.
You need a way to calculate the string length in pixels of the string (say, 120 pixels) and you need to know the page width in pixels.
At that point you position the string at x = (pagewidth/2 - stringwidth/2) and Bob's your uncle.
In FPDI you do this with GetStringWidth and GetPageWidth.
You can do the same adjustment with the height. For example you can split the string in words, calculate the width of each one, and determine when it is too much. This way you can split the string in two, and center each section.

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");

Placing two MultiCells next to each other using FPDF in PHP

I trying to create a custom table using FPDF Cell/MultiCell.
My 1st cell is a MultiCell that has two lines of text. The next cell should then just be placed right next to it.
Problem : no matter what I do to the next cell, it is always on the next line of the page instead of being placed right next to the 1st cell - and it's driving me crazy.
Here is my code:
require_once 'config.php';
require 'fpdf.php';
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','',10);
$pdf->MultiCell(150,10,'Certificate of foreign Currency usage in respect of materials and components in terms of the notes to rebate item ',1);
$pdf->SetFont('Arial','',10);
$pdf->MultiCell(40,10,'DA190',1);
$pdf->Output();
The cell containing the text "DA190" should be placed next to the previous cell, but is being positioned underneath the previous cell.
Before printing your first multicell, record the cursor position:
$x=$this->GetX();
$y=$this->GetY();
add your multicell using $this->Multicell($w,5,'Content');
Reset the cursor position to the start height (y) and the start horizontal + the width of the 1st multicell:
$this->SetXY($x+$w,$y);
Add your next multicell and repeat as necessary.
this worked for me
$pdf->multicell(120, 5, ' ' . $actividad, 0, 'l', true);
$x = $pdf->GetX();
$y = $pdf->GetY();
$pdf->SetXY($x + 120, $y);
$pdf->Cell(70, -5, ' ' . $claseActividad, '', 0, 'l', true);
result
I found the solution - fpdf has an extension (#3) focussed on using multicells.

Categories