Search code examples
phpmysqlfpdf

how to echo cod39 value inside $pdf->cell() or store it in variable and print the result inside a table?


I am trying to print a table in FPDF that has 4 columns, and many rows retrieved from MySQL.

Two of my columns shall print the value in barcode using code39 in FPDF. The problem is that it's not allowing me to print code39 results within the cells. I can do that only by adding the location to the $i and adding it to the location of the code39. Even though one page prints correctly, it then gets out of sync.

If you check on the output, you will see the problem I'm having.

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

The output:

the output


Solution

  • Before writing a cell that should contain a barcode, use the current cursor position in the document as the coordinates to draw the barcode

    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 
    }
    

    I haven't tested this. You might have to add or subtract an offset on GetX and GetY so that it looks good.