Add textfield inside Cell(TCPDF) - php

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

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.

Creating image with fix background and randomly displayed text from a pre-defined list

I would like to create a script which is generating an image with one fix background image but display a randomly selected sentence on the image from a list which is previously given in the script.
So far I have a base and I've tried many variations for the random text, but none of them worked until this time.
Here is the script itself:
<?php
header('Content-type: image/png');
$text = 'The text which will be displayed';
$background_path = 'assets/bg_image.jpg';
$image = imagecreatetruecolor(1280, 720);
$background = imagecreatefromjpeg($background_path);
$color = imagecolorallocate($image, 255, 255, 255);
$title_sizes = imagettfbbox(24, 0, 'assets/roboto-medium.ttf', $text);
$title_height = $title_sizes[3];
$title_width = $title_sizes[4];
imagecopy($image, $background, 0, 0, 0, 0, 1280, 720);
imagettftext($image, 24, 0, (640 - $title_width / 2), (360 - $title_height / 2), $color, 'assets/roboto-medium.ttf', $text);
imagepng($image);
imagedestroy($image);
imagedestroy($background);
?>
So the thing is that the line "$text" allows only one sentence to be displayed. I would like to have multiple lines (various sentences) instead which will be displayed on the given background image randomly upon refresh.
Can you please help me to solve this problem?
Thank you very much in advance!
Regards,
Adam

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

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

How to write bar codes into cells with tcpdf

I'll have to print bar codes and names into some labels (image below). The point is that the bar code never gets inside the cell. It is always outside.
The code I am using is here.
Below is the result:
Since you want the barcode to be visually inside the cell writing the name, you'll need to do a little positioning. The cell and barcode methods update the current position. If you write the barcode and then reset the position to what it was before the barcode call, and then write the name cell it should go somewhere inside the name cell.
//I'll provide the cell width in write1DBarcode to center the barcode.
$style['cellfitalign'] = 'C';
foreach ($pages as $pk => $p) {
// add a page
$pdf->AddPage();
foreach ($p as $lk => $l) {
foreach ($l as $ck => $c) {
//Get current write position.
$x = $pdf->GetX();
$y = $pdf->GetY();
// The width is set to the the same as the cell containing the name.
// The Y position is also adjusted slightly.
$pdf->write1DBarcode($c->id, 'C128B', '', $y-8.5, 105, 18, 0.4, $style, 'M');
//Reset X,Y so wrapping cell wraps around the barcode's cell.
$pdf->SetXY($x,$y);
$pdf->Cell(105, 51, $c->nome, 1, 0, 'C', FALSE, '', 0, FALSE, 'C', 'B');
}
$pdf->Ln();
}
}
This is the result I get now:

Categories