FPDF - PHP - Different styles on center cell - php

My problem is that I have a center text in a cell and I want the word "Client:" in bold and the rest in regular, like is centered I cant print "client:" first and after that print the name, neither use "write" function because is centered, please help.
$pdf->SetTextColor(102, 106, 117);
$pdf->SetFont('Arial', 'B', 15);
$pdf->Cell(626,25,"Client: ".$name,0,0,'C',0);

We have to calculate position of centered text like follows:
require("fpdf.php");
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetTextColor(102, 106, 117);
$fullCellWidth = $pdf->GetPageWidth();
$pdf->SetFont("Arial", "", 15);
$regularCell = "some name";
$regularWidth = $pdf->GetStringWidth($regularCell);
$pdf->SetFont("Arial", "B", 15);
$boldCell = "Client: ";
$boldWidth = $pdf->GetStringWidth($boldCell);
$centerIndentX = ($fullCellWidth - $boldWidth - $regularWidth) / 2;
$pdf->SetX($centerIndentX);
$pdf->Cell($boldWidth, 25, $boldCell, 0, 0, "L");
$pdf->SetX($centerIndentX + $boldWidth);
$pdf->SetFont("Arial", "", 15);
$pdf->Cell($regularWidth, 25, $regularCell, 0, 0, "L");
$pdf->Output();
The output PDF example - part of screenshot:

Related

Word wrap in a cell in FPDF using PHP?

I am trying to print some words in a cell but wordwrap in not working in a cell, what should i do?
$pdf->setXY(124, 36 + ($i * 5.1));
$pdf->SetFillColor(238, 236, 225);
$pdf->SetFont('Arial', '', 8);
$pdf->Cell(40, 6, $resultArrayIndex['pubtitle'], 0, 0, 'L', True);
Here is my output -
and here what i am trying to achieve - trying to wrap word within a cell. (Any help or hint is appreciated)
Edited after comments and sugggestions
After all u suggesting me use Multicell, I used it but it's still not helpful
not equal gap with each multicell and even sometimes it's uneven size text and gap
My code after your suggestion and i am using it in for-loop
$pdf->setXY(17, 36+($i * 6.9));
$pdf->SetFillColor(255,255,255);
$pdf->SetFont('Arial', '', 8);
$pdf->MultiCell(19, 4.6, formatPubDate($resultArrayIndex['pubdate']), 1);
$pdf->setXY(42, 36 + ($i * 6.9));
$pdf->SetFont('Arial', '', 8);
$pdf->MultiCell(50.5, 4.6, ($resultArrayIndex['title']), 1);
$pdf->setXY(124, 33.7 + ($i * 9.0));
$pdf->SetFont('Arial', '', 8);
$pdf-> MultiCell(27, 2.9 , $resultArrayIndex['pubtitle'],1);

SetDrawColor() is behaving like SetFillColor()

SetDrawColor() supposed to color Cell frame but instead another color is rendering as background color. This is the whole code :-
$pdf = new FPDF();
$pdf->SetTextColor(103, 58, 183);
$pdf->SetDrawColor(0, 80, 180);
// Fourth Page --
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(0, 10, "Hello Santanu", 1, 1, 'C', true);
$pdf->Output('helo.pdf', 'D');
You should try using SetFillColor before drawing any cell because it's taking black as default.
$pdf->SetFillColor(0, 0, 0);

php gd - multicolor imagettftext

I'm trying to put multicolor text through imagettftext.
I tried drawing letter by letter but the spacing is horrible.
Here's my code:
$usrname_split = str_split("MarioErmando");
$onecharwidth = imagefontwidth((int)$font)*(12/8);
foreach($usrname_split as $key=>$letter){
if($key == 0){
// first letter
imagettftext($im, 12, 0, $xusrname, 15, $blue, $font, $letter);
$oldletters = "$letter";
}else{
$posarr=imageftbbox(12, 0 ,$font, $oldletters);
$posx = $posarr["2"];
imagefttext($im, 12, 0, $posx, 15, $red, $font, $letter);
$oldletters .= "$letter";
}
}
The output:
Note that the text is dynamic.
Is it possible to achieve multicolor text through imagettftext without horrible spacing?
Regards, MarioErmando.
in order to test, replace your code by this one:
$string="Mario Ermando";
$last_string= substr( $string, 1 );
imagettftext($im, 12, 0, $xusrname, 20, $blue, $font, $string[0]); //first letter "M"
imagefttext($im, 12, 0, 12, 20, $red, $font, $last_string);
i think the problem is when you write every letter separately

Add textfield inside Cell(TCPDF)

I have a dynamic cell based on the user input, for example if the user wants 3 rows, it will display 3 rows on PDF. The problem is I need to make that cell fillable.
$pdf->setFormDefaultProp(array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 200), 'strokeColor'=>array(255, 128, 128)));
for ($r=1;$r<=$row;$r++) {
for ($c=1;$c<=$col;$c++) {
$w = 180/2;
$h = 200/$row;
//$nemsung2=$_POST['subjects'];
//$pdf->Cell($w, $h, '' .$nemsung2 ,1,0, 'L');
$pdf->SetTextColor(0,0,0);
$sample = $pdf->TextField('firstname', 50, 5);
$pdf->Cell($w, $h,$sample, 1,0, 'C');
My question is how can I add textfield inside a cell dynamically? thx

Using PHP GD to create image form text with different fonts

I have been using this simple script to generate images from text:
<?php
header('Content-type: image/png');
$color = RgbfromHex($_GET['color']);
$text = urldecode($_GET['text']);
$font = 'arial.ttf';
$im = imagecreatetruecolor(400, 30);
$bg_color = imagecolorallocate($im, 255, 255, 255);
$font_color = imagecolorallocate($im, $color[0], $color[1], $color[2]);
imagefilledrectangle($im, 0, 0, 399, 29, $bg_color);
imagettftext($im, 20, 0, 10, 20, $font_color, $font, $text);
imagepng($im);
imagedestroy($im);
function RgbfromHex($hexValue) {
if(strlen(trim($hexValue))==6) {
return array(
hexdec(substr($hexValue,0,2)), // R
hexdec(substr($hexValue,2,2)), // G
hexdec(substr($hexValue,4,2)) // B
);
}
else return array(0, 0, 0);
}
?>
I call the script with file.php?text=testing script&color=000000
Now I'd like to know how could I generate text with normal and bold fonts mixed in the same image, something like file.php?text=testing <b>script</b>&color=000000
Thanks to dqhendricks for helping me figure this out.
Here's a quick script I wrote, still needs lot of improvements but for the basic functionality it seems to be working fine:
<?php
header('Content-type: image/png');
$color = RgbfromHex($_GET['color']);
$im = imagecreatetruecolor(400, 30);
$white = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$tmp = $_GET['text'];
$words = explode(" ", $tmp);
$x = array(0,0,10); // DUMMY ARRAY WITH X POSITION FOR FIRST WORD
$addSpace = 0;
foreach($words as $word)
{
if($addSpace) $word = " ".$word; // IF WORD IS NOT THE FIRST ONE, THEN ADD SPACE
if(stristr($word, "<b>"))
{
$font = 'arialbd.ttf'; // BOLD FONT
$x = imagettftext($im, 20, 0, $x[2], 20, imagecolorallocate($im, $color[0], $color[1], $color[2]), $font, str_replace(array("<b>","</b>"), "", $word));
}
else
{
$font = 'arial.ttf'; // NORMAL FONT
$x = imagettftext($im, 20, 0, $x[2], 20, imagecolorallocate($im, $color[0], $color[1], $color[2]), $font, $word);
}
$addSpace = 1;
}
imagepng($im);
imagedestroy($im);
function RgbfromHex($hexValue) {
if(strlen(trim($hexValue))==6) {
return array(
hexdec(substr($hexValue,0,2)), // R
hexdec(substr($hexValue,2,2)), // G
hexdec(substr($hexValue,4,2)) // B
);
}
else return array(0, 0, 0);
}
?>
Note: This will only work for "bolding" single words separated by spaces and not for bolding part of a word.
Call the script with file.php?text=testing+<b>script</b>&color=000000
you will need to load an arial-bold font file, and do two separate imagettftext() calls, one with each font you want to use. as for parsing the string to find out which parts you would like to be bold, and where you should print each section of text, this sounds like it will become very complicated code. what are you even using this for? there may be better solutions for accomplishing the same thing.
Addition
Use the return value from the imagettftext() function to determine where the next text print should start.
From documentation:
Returns an array with 8 elements representing four points making the bounding box of the text. The order of the points is lower left, lower right, upper right, upper left. The points are relative to the text regardless of the angle, so "upper left" means in the top left-hand corner when you see the text horizontally. Returns FALSE on error.
Response for:
The parsing wouldn't be a problem, what I don't have clear is how I would find the X position for the second wordr in the example ?text=testing script. I mean how do I know where the first word ends so I can place the second one there with another imagettftext() and a bold font. – Meredith Jan 9 '11 at 2:43
funny how someone took the time to say "see edits for the answer to that" when they coulda just said to explode the string at the spaces, then each word is in an array..
<?PHP
$sentence = "I LOVE giving retarded answers that don't amount to jack!";
$sentence = explode(" ",$sentence );
for($s=0;$s<count($sentence);$s++){
#THERES YOUR INDIVIDUAL WORDS!
$word = $sentence[$s];
}
?>
Your X position would be simply $s for your logic. To get the second word you can do this:
<?PHP
$word1 = $sentence[0];
$word2 = $sentence[1];
$word3 = $sentence[2];
$word4 = $sentence[3];
?>
Yes, i named the $words just for a mental visual effect.
$sentence[1] would be word 2

Categories