How to auto resize the cell in fpdf using php - php

I need to auto adjust the cell size depends upon the text . I have the following code .
$pdf->Cell(50,10,$name,1,0,'L',0);
If the $name exceed 10 character means the $name wrap into the next cell. How to fix?

I refer to: GetStringWidth and SetFont. As pointed out there, to use this method, a font must be selected. So I assume, you already have something like:
.
.
$pdf->AddPage();
$fontFamily = 'Courier'; // 'Courier', 'Helvetica', 'Arial', 'Times', 'Symbol', 'ZapfDingbats'
$fontStyle = 'B'; // 'B', 'I', 'U', 'BI', 'BU', 'IU', 'BIU'
$fontSize = 12.0; // float, in point
$pdf->SetFont($fontFamily, $fontStyle, $fontSize);
To adjust the cell width to the length of the text, let its value take the calculated length of the text:
$width = $pdf->GetStringWidth($name);
$height = 10.0;
$border = 1;
$ln = 0;
$align = 'L';
$fill = FALSE;
$pdf->Cell($width, $height, $name, $border, $ln, $align, $fill);
Havn't tested it, just read the manual. Hope it works.

you can use this code to solve this problem... here the concept is only if string size greater than your cell width then font size will reduce.
$pdf->CellFit($table_head2[7],$table_height,$item['remark'],1,0,'C',0,'',1,0);
check this URL
how to auto adjust cell width in fpdf using php and mysql

Related

how to center and align texts by merging two images using PHP GD

How do I centralize and align text above my main image?
The result of this code is this image:
https://i.imgur.com/Dk5pYJM.jpg
I wish it would look like this:
https://i.imgur.com/mED295l.jpg
but I do not understand much of moving the images and texts.
<?php
$fontname = 'verdana.ttf';
$i = 30;
$quality = 85;
function create_image($user){
global $fontname;
global $quality;
$file = md5($user[1]['text'].rand(30454, 343434)).".jpg";
//if (!file_exists($file)) {
$im = imagecreatefromjpeg("fundo.jpg");
$logo = imagecreatefromjpeg("img.jpeg");
$color['grey'] = imagecolorallocate($im, 255, 255, 255);
$y = imagesy($im) - $height - 365;
$font_size = 25;
$logo_x = imagesx($logo);
$logo_y = imagesy($logo);
foreach ($user as $value){
$x = center_text($value['text'], $font_size);
imagettftext($im, $font_size, 0, $x, $y+$i, $color["grey"], $fontname, $value['text']);
$i = $i+32;
}
imagecopymerge($im, $logo, 37, 370, 0, 0, $logo_x, $logo_y, 100);
imagejpeg($im, $file, $quality);
//}
return $file;
}
function center_text($string, $font_size) {
global $fontname;
$image_width = 720;
$dimensions = imagettfbbox($font_size, 0, $fontname, $string);
return ceil(($image_width - $dimensions[4]) / 2);
}
$user = array(
array('text'=> 'Our adge lacks gravitas. That’s whyaadasdasdasdasdsadasdasdasdas')
);
$filename = create_image($user);
?>
<img src="<?=$filename;?>" /><br/><br/>
Aligning text in an image using PHP's image functions is always a bit of a puzzle.
First, you need to know the x and y coordinates of the point at the bottom center of where the text should go (anchor point). This is probably half the width of the outer image for the x and the y value of the top of the smaller image in the center (minus a margin value) for the y.
After this, you have to calculate where your text has to be put (x and y) so that it will align above the smaller image in the center (centered above the anchor point).
For this you use imagettfbbox to calculate what the bounding box's coordinates will be if the text is put at x=0 y=0. Then you use these values to calculate the coordinates of the bottom center of this bounding box.
$bounding_box = imagettfbbox(......);
/* lower right x - lower left x, divided by 2 */
$relative_x = ( $bounding_box[2] - $bounding_box[0] ) / 2;
/* lower left y */
$relative_y = $bounding_box[1];
Subtracting these relative values from our first calculated anchor point, gives you the coordinates to put the text using imagettftext.
See http://php.net/manual/en/function.imagettfbbox.php for which array value is which coordinate value.

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

Right allignment of text in imagettftext function

I am creating images with some text, when rotation is 0 then its fine but when rotation is > 0 then image looks are bad due to text alignment, because by default alignment is left.Since each and every value is dynamic so we can not fix it,please help me.
$message = 'Thanks, Deep, for making time to have a coffee today Purushottam';
$no_of_characters_line = $temp_data['no_of_characters_line'];
$lines = explode('|', wordwrap($message, $no_of_characters_line, '|'));
// Starting Y position and X position
$y = $temp_data['position_from_top'];
$x = $temp_data['position_from_left'];
$font_size = $temp_data['font_size'];
$rotation_angle = $temp_data['rotation'];
$line_height = $temp_data['line_height'];
foreach ($lines as $line)
{
imagettftext($im, $font_size,$rotation_angle, $x, $y, $black, $font, $line);
// Increment Y so the next line is below the previous line
$y += $line_height;
}
I am also attaching example template.
You can use imagettfbbox to calculate the string size in pixels. Then, with the width you will be able calculate the offset required to align the text to the right. Also, check out the imagettftext documentation for some examples of text alignment.

Getting text height to know fill height as in TCPDF

I'm trying to go through the code of TCPDF to understand how it calculates the height of the text to be rendered, but it's too much for me to handle without asking.
What I want to know: in the PDF from example 5 http://www.tcpdf.org/examples/example_005.pdf it gives the cell a yellow background. I'm guessing that at the basic level, it first draws a box with this fill color, then adds the text, so what method is it calling to get the height of the text to know the height of the box to fill?
I can see from the example code that MultiCell() is the entry point, but it's not clear what's the method it calls to get the height of the text. I pasted the code for MultiCell() in this pastebin
http://pastebin.com/A1niGrQG
Anyone knows how to trace this, because doing it by hand and looking through the code isn't working at all for me.
TCPDF (at least the latest version) includes the method getStringHeight() which get the estimated height needed for printing a simple text string using the Multicell() method.
Additionally, the getNumLines() method gives you the estimatad number of lines.
Check the source code documentation at http://www.tcpdf.org for further information.
The cell is being drawn by MultiCell:
http://www.tcpdf.org/examples/example_005.phps
$pdf->MultiCell(55, 5, '[LEFT] '.$txt, 1, 'L', 1, 0, '', '', true);
and from: http://api.joomla.org/com-tecnick-tcpdf/TCPDF.html
int MultiCell (float $w, float $h, string $txt, [mixed $border = 0], [string $align = 'J'], [int $fill = 0], [int $ln = 1], [int $x = ''], [int $y = ''], [boolean $reseth = true], [int $stretch = 0])
So as you can see, the first two values are statically assigning a width (55) and a height (5) to the MultiCell
Additionally:
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
You can see the unit of measurement is the program/class default PDF_UNIT
The font size is then set with
$pdf->SetFont('times', '', 10);
(or just use SetFontSize for the size only)
Just as a very simple proof of concept for what i said in my last comment on my other answer...
** You need to use a monospaced font and a line height equal to your text height (that or modify the code for line height instead of text height) fairly simple fix...
You also have to figure out your aproximate monospaced width.. Best thing to do is use a capitol M (M is the widest char - so monospaced chars are set to this width..)
<html><head></head><body style="font-family:'Courier New', Courier, monospace; line-height:12px;">
<?php
//If you are using a monospace font, this kinda works
$divWidth = 300; // in px;
$fontSize = 12; // (in px);
$fontWidth = 7; // in px - aprox monospace font width
$lineChars = floor($divWidth / $fontWidth);
$text = <<<EOT
MMMMMMMMMM (capital M is the widest character)I'm trying to go through the code of TCPDF to understand how it calculates the height of the text to be rendered, but it's too much for me to handle without asking.
What I want to know: in the PDF from example 5 it gives the cell a yellow background. I'm guessing that at the basic level, it first draws a box with this fill color, then adds the text, so what method is it calling to get the height of the text to know the height of the box to fill?
I can see from the example code that MultiCell() is the entry point, but it's not clear what's the method it calls to get the height of the text. I pasted the code for MultiCell() in this pastebin
EOT;
$wrappedText = wordwrap($text, $lineChars, "LINEHERE");
$lines = substr_count($wrappedText, "LINEHERE");
$newlines = substr_count($text, "\n");
$text = str_replace("\n", "<br>",$text);
$lines += $newlines;
$divHeight = $lines * $fontSize;
echo "With a width of: " . $divWidth . "<br>";
echo "Number of Lines: " . $lines . "<br>";
echo "Height Required: " . $divHeight . "px<br>";
echo "Wrapped Text at: " . $lineChars . " characters<br><br>";
$divsize = "width:$divWidth px; height:$divHeight px; font-size:$fontSize px; ";
$outStr = "<div style='overflow:auto; display:inline-block; background-color:aqua; $divsize'>$text</div>";
$outStr .= "<div style=' display:inline-block; background-color:fuchsia; $divsize'> </div>";
echo $outStr;
?>
</body></html>

Categories