How to write bar codes into cells with tcpdf - php

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:

Related

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

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 scale bottom-of-page text using TCPDF and keep it on the page?

I'm using TCPDF to create barcode labels. The code works great until I tried to scale the price text to make it taller (more visible). Now the bottom row of labels are spilling components onto later pages, including components that are not scaled. It appears the ordinate value of the text along the bottom row of labels is off the bottom of the page and that is causing the problem, but I thought that setting the (x,y) value when calling procedures like MultiCell absolutely set the (x,y) value?. Does anyone know how to fix this? Can I force a large art board? Can I move the object after it has been scaled? Can I somehow recover from the off-page ordinate calculation?
The issue is in the create_label function, below. The second MultiCell line is the scaled entry. The specific effect on the bottom row of labels is that the description and price are correctly on the first label, but the barcode is forced to a second page. The description and price of the second label are on the second page, but the barcode is forced to a third. Etc.
function create_label($posx, $posy, $upc, $price, $desc){
global $START_X, $START_Y, $INC_X, $INC_Y, $style, $pdf;
$pdf->SetFont('helvetica', '', 15);
$pdf->MultiCell(1.00, 0.575, $desc, 0, 'L', false, 1, $START_X+($posx*$INC_X), $START_Y+($posy*$INC_Y), true, 1, false, true, 0.575, 'M', true);
$pdf->SetFont('', 'B', '24');
$pdf->StartTransform();
$pdf->ScaleY('200',1.00,0.7+($posy*$INC_Y)); break;
$pdf->MultiCell(1.00, 0.575, $price, 0, 'R', false, 1, $START_X+($posx*$INC_X)+1.00, $START_Y+($posy*$INC_Y), true, 1, false, true, 0.575, 'M', true);
$pdf->StopTransform();
$pdf->write1DBarcode($upc, 'UPCA', $START_X+($posx*$INC_X), $START_Y+($posy*$INC_Y)+0.3125+0.2625, 2.0, .3, '', $style, 'N');
}
$pdf = new TCPDF('P', 'in', 'LETTER', true, 'UTF-8', false);
$pdf->SetMargins(0.1875, 0.5, 0.1875);
$pdf->SetHeaderMargin(0);
$pdf->SetFooterMargin(0);
$pdf->SetAutoPageBreak(TRUE, 0.5);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
if (#file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
$pdf->AddPage();
$START_X = 0.85;
$START_Y = 0.38;
$INC_X = 2.375;
$INC_Y = 0.9375;
// label width = 2.25
// label height = 0.875
// Label gutter = 1/8 x 1/16
$upc_list = array( /*Array of UPC numbers*/ );
for($row = 0; $row <= 10; $row++){
for ($col = 0; $col <= 2; $col++){
unset($upc);
if(strlen($upc) > 0 && strlen($upc) < 13 && is_numeric($upc)) {
unset($item);
$item = get_item_from_database($upc); // Creates data array
create_label($col, $row, $upc, $item['price'], trim($item['description']));
}
}
}
$pdf->Output('test_pdf.pdf', 'I');

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

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