0

I have created an FPDF PDF document that puts an image on label paper which has 9 rows of labels (PHP code below)

There is no space/gap between each row of labels, and the next row of labels start immediately after the previous row.

### THE ISSUE: ###

When the labels are printed, the image displayed on each label moves down slightly from the top of each label, causing the bottom row of labels (9th row) to be significantly different to the 1st couple of rows.

I need to add additional content to each label, and if this issue continues, some of this content is going to get cut off.

I don't understand why this is happening, and can't see anything obvious in the code. Can anyone here spot what I'm doing wrong?

My code.....

use Fpdf\Fpdf as FPDF;

$current_x_position = 0;
$current_y_position = 0;

$total_y_per_page = 9;
$total_x_per_page = 3;

$pdf = new FPDF();
$pdf->SetMargins(0,0);
$pdf->SetAutoPageBreak(false);
$pdf->AddPage();

for($qty = 1; $qty <= 10; $qty++) {

    label($current_x_position, $current_y_position, $pdf);

    $current_y_position++;

    if($current_y_position == $total_y_per_page) {

        $current_x_position++;
        $current_y_position = 0;

        if($current_x_position == $total_x_per_page) {

            $current_x_position = 0;
            $current_y_position = 0;

            $pdf->AddPage('P');

        }

    }

}

$pdf->Output();

function label($current_x_position, $current_y_position, $pdf) {

    $left_margin = 7;
    $top_margin = 15;
    $label_width = 66;
    $label_height = 30;

    $current_x_position = $left_margin + ($label_width * $current_x_position);
    $current_y_position = $top_margin + ($label_height * $current_y_position);

    $pdf->Image('image.png', $current_x_position, $current_y_position+=1, $label_width, 10, 'png');

    return;

}

?>
Ea2018
  • 1
  • 1

1 Answers1

0

I think the problem stems from how x is being handled. It should be distinct from y. Why? Because x needs to be reset after every 3rd label is printed. Since it is only tested and incremented after 9 rows are printed, it will surely cause slueing. (Maybe that's an old-timey expression, used to indicate a program causes a new line because it is already past the column in which you want to print). Suggest you need to test x after every label print, so it can be reset to 0 when it reaches total_x_per_page (which I think really means total_x_per_row). The x test needs to be independent of the y test; it needs to be before the y test because you the columns will be complete before the rows.

In pseudo-ish code:

  • print a label
  • if the row is complete reset x, otherwise increase x
  • if the page is complete, reset y and x.

My experience with Fpdf is nil. My experience with (fighting) labels goes back decades :)

DinoCoderSaurus
  • 6,110
  • 2
  • 10
  • 15