FPDF第二页首先不匹配

时间:2014-07-28 21:20:47

标签: php fpdf

我正在尝试使用FPDF输出一堆QRCode,以便保存并打印出来。我正在复制的打印格式是Avery 6572模板。

我的问题是输出的第一页绝对是原始的,但随后的页面在Y位置完全被破坏,每个QR码都在它自己的页面上。

老实说,我花了几天时间搞乱Y位置,试图通过表输出我的数据,尝试用MultiCell()方法输出,一切都无济于事......我能得到的最接近的是以下:http://www.galatium.net/qrcodes.pdf

有人可以对我失踪的内容有所了解吗?

以下是我的代码,如果你想复制它(注意你需要你自己的图像)

require_once('../fpdf/fpdf.php');

//$intids = explode(",", $_POST['intids']);

//Add 15 manually for the purpose of testing
$intids = array(
            -62016037,21924259,-768047323,1429473130,164704222,
            1815464709,-1787257749,621338038,-1511797412,-2134836004,
            2128460384,-180522171,-790468982,1589074358,-1227881486
        );

//Add 4 more to show brokenness
$intids[] = -62016037; //16
$intids[] = 21924259; //17
$intids[] = -768047323; //18
$intids[] = 1429473130; //19


//Verify there are intids, and that the first one is not empty
if (count($intids) > 0 && !empty($intids[0])) {
    $topMargin = 12; //Set document top margin to match Avery 6572 Template
    $labelWidth = 70; //Approx. width in mm of the label
    $labelHeight = 54; //Approx. height in mm of the label

    $pdf = new FPDF('P', 'mm', 'Letter');
    $pdf->AddPage();
    $pdf->SetMargins(0, $topMargin, 0);
    $pdf->SetAutoPageBreak(true, 0); //Set bottom margin to 0

    $counter = 0;
    $col = 0;
    $row = 0;
    $y = $topMargin;

foreach ($intids as $intid) {
    if (is_numeric($intid)) {
        ++$counter;

        $pdf->SetY($y);
        //Adding 4 to replicate paper Avery Template margins
        if ($col) $pdf->SetX($pdf->GetX() + 4 + ($labelWidth * $col));

        $pdf->SetFont('Arial', '', 14);
        $pdf->Cell($labelWidth, 5, "Some Title for QRCode #".$counter."", 0, 1, 'C');

        if ($col) $pdf->SetX($pdf->GetX() + 4 + ($labelWidth * $col));

        $pdf->SetFont('Arial', 'B', 13);
        $pdf->Cell($labelWidth, 5, "Brief Content for #".$counter, 0, 1, 'C');

        if ($col) $pdf->SetX($pdf->GetX() + 4 + ($labelWidth * $col));

        //Image size: 135x135px Added 17mm to X for centering
        $pdf->Image('images/qrcodes/' . $intid . '.png', $pdf->GetX() + 17, $pdf->GetY());

        $pdf->SetY($pdf->GetY() + 36);
        if ($col) $pdf->SetX($pdf->GetX() + 4 + ($labelWidth * $col));

        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell($labelWidth, 5, "Small Text for #".$counter, 0, 0, 'C');

        //If the third column, reset column count and set Y to start at next line
        if (++$col == 3) {
            $y = ($labelHeight * ++$row) + $topMargin;
            $col = 0;
        }
    }
}


$pdf->Output();
}

1 个答案:

答案 0 :(得分:0)

感谢bmb带领我走下自动分页路径。

对于未来的访客和好奇的人,解决方案如下:

设置$pdf->AutoPageBreak(false, 0);

然后改变这些行:

if (++$col == 3) {
    $y = ($labelHeight * ++$row) + $topMargin;
    $col = 0;
}

要:

if (++$col == 3) {
    $y = ($labelHeight * ++$row) + $topMargin;

    if ($row > 0 && $row % 5 == 0) {
        $pdf->AddPage();
        $y = $topMargin;
        $row = 0;
    }

    $col = 0;
}