Filling in PDF from web form in PHP

时间:2016-04-04 18:19:17

标签: symfony fpdf

I have a form which on posting submits data to a controller which should then fill necessary fields and present form to users to physically sign before submitting back.

I used Dhek https://github.com/cchantep/dhek do define the fields on which the form data will be placed and FPDF to generate the final PDF for downloading. I am able to get selected checkboxes marked but no text fields are rendered. Here is what I have tried so far

    $json = json_decode(file_get_contents($this->getRequest()->getUriForPath('/bundles/app/form.json')));
    $responses = $request->query->all();

    $pdfSrcPath = $this->container->getParameter('write_to') . '/bundles/app/Membership_App__form.pdf';

    $pdf = new \FPDF_FPDI("P", //L=>Landscape / P=>Portrait
      "pt" /* point */ );

    $fontSize = 14;

    $pagecount = $pdf->setSourceFile($pdfSrcPath);
    $testText  = "abcdefghijklmnopqrstuvwxyz0123456789";

    for ($i = 0; $i < $pagecount; $i++) 
    {
        $pdf->AddPage();
        $tplIdx = $pdf->importPage($i + 1);
        $pdf->useTemplate($tplIdx, 0, 0, 0, 0, true);

        if (isset($json->pages[$i]) && isset($json->pages[$i]->areas)) 
        {

            for ($j = 0; $j < count($json->pages[$i]->areas); $j++) 
            {
                $area = $json->pages[$i]->areas[$j];
                $x    = $area->x;
                $y    = $area->y;
                $w    = $area->width;
                $h    = $area->height;

                // Draw blue rect at bounds
                $pdf->SetDrawColor(0, 0, 255);
                $pdf->SetLineWidth(0.2835);
                $pdf->Rect($x, $y, $w, $h);

                if ($area->type == "checkbox" && $area->name == $responses['title']) 
                {                       
                    $pdf->SetDrawColor(105, 105, 105);
                    $pdf->SetLineWidth(2.0);
                    $pdf->Line($x, $y, $x + $w, $y + $h);
                    $pdf->Line($x, $y + $h, $x + $w, $y);
                } 
                else if ($area->type == "text") 
                {
                    // 'Free' text
                    $pdf->SetLineWidth(1.0); // border

                    $iw       = $w - 2 /* 2 x 1 */ ;
                    $v        = utf8_decode($responses[$area->name]);
                    $overflow = ($pdf->GetStringWidth($v) > $iw);
                    while ($pdf->GetStringWidth($v) > $iw) 
                    {
                        $v = substr($v, 0, -1);
                    }
                    if ($overflow) 
                    {
                        $v = substr($v, 0, -1) . "\\";
                    }

                    $pdf->SetXY($x, $y);
                    // this line is not rendering
                    // tried $pdf->Write(intval($h),$v);
                    // and also tried $pdf->Cell($w, intval($h), $v);
                    $pdf->MultiCell($w, intval($h), $v, true);
                }
            }
        }
    }
    $pdf->Output("test-dhek.pdf", "F");

0 个答案:

没有答案