Almino Melo
Almino Melo

Reputation: 351

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 adesive labels. I have to print a barcode in which square (10 per page).

The code I am using is here.

Below is the result: enter image description here

Upvotes: 2

Views: 9446

Answers (1)

EPB
EPB

Reputation: 4029

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:

This is the result I get now

Upvotes: 3

Related Questions