首页 > 解决方案 > 如何在 $pdf->cell() 中回显 cod39 值或将其存储在变量中并在表格中打印结果?

问题描述

我正在尝试在 FPDF 中打印一个有 4 列的表,并且从 MySQL 检索到许多行。

我的两列应使用code39FPDF 打印条形码中的值。问题是它不允许我code39在单元格中打印结果。我只能通过将位置添加到$i并将其添加到code39. 即使一页打印正确,它也会不同步。

如果您检查输出,您将看到我遇到的问题。

foreach ($result as $row) {
    
    $pdf->Code39(12,47+$i,'%'.$row['activity'].' START',1,10);
    $pdf->Code39(209,47+$i,'%'.$row['activity'].' END',1,10);
    
    $pdf->Cell($width_cell[0],20,'',1,0,'c',false); // First column of row 1 
    $pdf->Cell($width_cell[1],20,$row['activity'],1,0,'c',false); // Second column of row 1 
    $pdf->Cell($width_cell[2],20,$row['a_description'],1,0,'c',false); // Third column of row 1 
    $pdf->Cell($width_cell[3],20,'',1,1,'c',false); // Fourth column of row 1 
    $i=$i+20;    
}

输出:

输出

标签: phpmysqlfpdf

解决方案


在编写一个应该包含条形码的单元格之前,使用文档中当前光标位置作为坐标来绘制条形码

foreach ($result as $row) {    

    $pdf->Code39($pdf->GetX(), $pdf->GetY(), '%'.$row['activity'].' START',1,10);
    $pdf->Cell($width_cell[0],20,'',1,0,'c',false); // First column of row 1 

    $pdf->Cell($width_cell[1],20,$row['activity'],1,0,'c',false); // Second column of row 1 
    $pdf->Cell($width_cell[2],20,$row['a_description'],1,0,'c',false); // Third column of row 1 

    $pdf->Code39($pdf->GetX(), $pdf->GetY(), '%'.$row['activity'].' END',1,10);
    $pdf->Cell($width_cell[3],20,'',1,1,'c',false); // Fourth column of row 1 
}

我没有测试过这个。您可能必须在 GetX 和 GetY 上添加或减去偏移量,以使其看起来不错。


推荐阅读