如何使用tcpdf将条形码写入单元格

时间:2013-05-06 21:11:52

标签: php tcpdf

我必须在一些标签上打印条形码和名称(下图)。关键是条形码永远不会进入细胞内部。它总是在外面。

The adesive labels. I have to print a barcode in which square (10 per page).

我使用的代码是here

以下是结果: enter image description here

1 个答案:

答案 0 :(得分:3)

由于您希望条形码可视地在单元格内写入名称,因此您需要进行一些定位。单元格和条形码方法更新当前位置。如果您编写条形码,然后将位置重置为条形码调用之前的位置,然后写入名称单元格,它应该位于名称单元格内的某处。

//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