Symfony - FPDI无法在低于限制的情况下插入图像

时间:2017-06-14 06:32:34

标签: php symfony pdf fpdi

我必须在现有PDF上写一些文字和图像。此PDF由2页组成,这两页是10张卡片的两面,宽度为2张卡,高度为5张卡。 我为此目的使用FPDI。我还有最后一个问题:我无法插入低于y = 232限制的图像(或文本)。如果我想将其插入1 px以下,则图像会显示在新页面上。如何在不进入新页面的情况下将图像插入到我想要的位置? 页面是A4,所以它应该工作。

我的代码:

    $em = $this->getDoctrine()->getManager();

    $users = $em->getRepository(Admin::class)->findAll();

    $pdf = new \FPDI();
    $pageCount = $pdf->setSourceFile($this->get('kernel')->getRootDir() . '/../web/bundles/pacesuser/pdf/Cartes.pdf');

    // Variable to group users by 10 for loop
    $iteration = 0;

    // 1st card element positions
    $xNom = 33.5;
    $yNom = 24.5;
    $xPrenom = 39;
    $yPrenom = 30.5;
    $xNumero = 49;
    $yNumero = 36;
    $xImage = 66.5;
    $yImage = 14.5;

    // For each group of 10 users, add pages of initial PDF
    for ($i = 1; $i <= count($users)/10 +1; $i++) {
        // Iterate through pages of initial PDF
        for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
            // import a page
            $templateId = $pdf->importPage($pageNo);
            // get the size of the imported page
            $size = $pdf->getTemplateSize($templateId);

            // create a page (A4 format)
            $pdf->AddPage('P', array($size['w'], $size['h']));

            // use the imported page
            $pdf->useTemplate($templateId);

            // If page 1
            if ($pageNo == 1) {

                // Variables to know in which row we are
                $rowPaire = 0;
                $rowImpaire = 0;

                // Condition to solve the last page problem (undefined index)
                if (count($users) >= $iteration+10) {
                    // For each user in a group of 10 users
                    for ($userNo = $iteration; $userNo < $iteration + 10; $userNo++) {
                        // If loop first
                        if ($userNo == $iteration) {
                            $pdf->SetFont('Helvetica', '', 10);
                            $pdf->SetXY($xNom, $yNom);
                            $pdf->Write(10, $users[$userNo]->getNom());
                            $pdf->SetXY($xPrenom, $yPrenom);
                            $pdf->Write(10, $users[$userNo]->getPrenom());
                            $pdf->SetFont('Helvetica', '', 12);
                            $pdf->SetXY($xNumero, $yNumero);
                            $pdf->Write(10, $users[$userNo]->getUsername());
                            $pdf->Image($this->get('kernel')->getRootDir() . '/../web/uploads/documents/section8-image.png', $xImage, $yImage, 33.5, 45);
                            $rowImpaire += 1;
                        }
                        // Elseif loop even
                        elseif ($userNo % 2 == 0) {
                            $pdf->SetFont('Helvetica', '', 10);
                            $pdf->SetXY($xNom, $yNom+$rowImpaire*55.5);
                            $pdf->Write(10, $users[$userNo]->getNom());
                            $pdf->SetXY($xPrenom, $yPrenom+$rowImpaire*55.5);
                            $pdf->Write(10, $users[$userNo]->getPrenom());
                            $pdf->SetFont('Helvetica', '', 12);
                            $pdf->SetXY($xNumero, $yNumero+$rowImpaire*55.5);
                            $pdf->Write(10, $users[$userNo]->getUsername());
                            $pdf->Image($this->get('kernel')->getRootDir() . '/../web/uploads/documents/section8-image.png', $xImage, 231, 33.5, 45);
                            $rowImpaire += 1;
                        }
                        else {
                            $pdf->SetFont('Helvetica', '', 10);
                            $pdf->SetXY($xNom + 87, $yNom + $rowPaire * 55.5);
                            $pdf->Write(10, $users[$userNo]->getNom());
                            $pdf->SetXY($xPrenom + 87, $yPrenom + $rowPaire * 55.5);
                            $pdf->Write(10, $users[$userNo]->getPrenom());
                            $pdf->SetFont('Helvetica', '', 12);
                            $pdf->SetXY($xNumero + 87, $yNumero + $rowPaire * 55.5);
                            $pdf->Write(10, $users[$userNo]->getUsername());
                            $pdf->Image($this->get('kernel')->getRootDir() . '/../web/uploads/documents/section8-image.png', $xImage + 87, $yImage + $rowPaire * 55.5, 33.5, 45);
                            $rowPaire += 1;
                        }
                    }
                }
                // Last page loop
                else {
                    for ($userNo = $iteration; $userNo < count($users); $userNo++) {
                        <!-- Same code as above -->
                    }
                }
                $iteration += 10;
            }
        }
    }

// Output the new PDF
    $pdf->Output();

1 个答案:

答案 0 :(得分:0)

使用SetAutoPageBreak()禁用自动分页符或更改底部边距。