首页 文章

使用PHP FPDF Library创建的标签向下滑动页面

提问于
浏览
0

我创建了一个FPDF PDF文档,将图像放在标签纸上,标签纸上有9行标签(下面是PHP代码)

每行标签之间没有空格/间隙,下一行标签紧接在前一行之后 .

### THE ISSUE: ###

打印标签时,每个标签上显示的图像从每个标签的顶部稍微向下移动,导致底行标签(第9行)与第1行显着不同 .

我需要为每个标签添加额外的内容,如果这个问题仍然存在,这些内容中的一部分将会被切断 .

我不明白为什么会这样,并且在代码中看不到任何明显的东西 . 谁能在这里发现我做错了什么?

我的代码.....

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;

}

?>

1 回答

  • 0

    我认为问题源于如何处理 x . 它应与 y 截然不同 . 为什么?因为在打印每个第3个标签后需要重置 x . 由于它仅在打印9行后进行测试和增加,因此肯定会引起污染 . (也许这是一个古老的表达式,用于表示程序导致新行,因为它已经超过了您要打印的列) . 建议你需要在每个标签打印后测试 x ,所以当它达到 total_x_per_page 时可以重置为0(我认为这实际上意味着 total_x_per_row ) . x 测试需要独立于 y 测试;它需要在 y 测试之前,因为列将在行之前完成 .

    在伪代码中:

    • 打印标签

    • 如果行完成重置x,否则增加x

    • 如果页面完成,请重置y和x .

    我对Fpdf的经验是零 . 我对(战斗)标签的体验可以追溯到几十年前:)

相关问题