使用fpdf将表单内容显示为pdf

时间:2012-11-15 10:10:32

标签: php fpdf

我的表格得到了回应:

echo "<form action= 'mypdf.php' method='post' name='print' target='_blank' >";
for($x=0; $x<$N; $x++){
        echo nl2br("<tr>
                      <td><textarea name=unit[]>$bute[$x]</textarea></td>
                      <td><textarea name=final[]></textarea></td>
                      <td><textarea name=act[];></textarea></td> 
}
echo "<input type='submit' name='Submit' value='Submit'></br></form>";

我在为FPDF开发一个参数以显示表单内容时遇到问题。表单的内容不存储在数据库中。

<?php
require('fpdf.php');
class PDF extends FPDF {

    function BuildTable($header,$data) {

    $this->SetFillColor(255,0,0);

    $this->SetTextColor(255);

    $this->SetDrawColor(128,0,0);

    $this->SetLineWidth(.3);

    $this->SetFont('','B');
    $w=array(85,40,15);
    for($i=0;$i<count($header);$i++)

    $this->Cell($w[$i],7,$header[$i],1,0,'C',1);

    $this->Ln();
    $this->SetFillColor(175);

    $this->SetTextColor(0);

    $this->SetFont('');
    $fill=true; 

    foreach($data as $row)

    {
    $this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
    $this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
$this->Cell($w[2],6,$row[2],'LR',0,'C',$fill);

    $this->Ln();

   $fill =! $fill;

    }
    $this->Cell(array_sum($w),0,'','T');

    }

}

$data[] = array(($_POST['unit']), ($_POST['final']), (['act']));
$pdf = new FPDF('L', 'in', 'A4');
$header=array('Attributes','Outcome','Activites');

$pdf->SetFont('Arial', 'B', 10);

$pdf->AddPage();
$pdf->BuildTable($header,$data);
$pdf->Output();
?>

请有人帮助他们。感谢

2 个答案:

答案 0 :(得分:1)

您的代码

$data[] = array(($_POST['unit']), ($_POST['final']), (['act']));

将其更改为

$data[] = array(($_POST['unit']), ($_POST['final']), ($_POST['act']));

尝试或

$unit = $_POST['unit'];
$final = $_POST['final'];
$act  = $_POST['act'];

for($i = 0; $i< count($unit); $i++){
    $data[] = array(($unit[$i]), ($final[$i]), ($act[$i]));
}

答案 1 :(得分:1)

我试试这个。但差别不大。但我认为你可以从这段代码中找到解决方案。

index.php
<form action="mypdf.php" method="post">
    <table>
       <tr>
         <td><textarea name="unit[]"></textarea></td>
         <td><textarea name="final[]"></textarea></td>
         <td><textarea name="act[]"></textarea></td>
      </tr>
       <tr>
        <td><textarea name="unit[]"></textarea></td>
        <td><textarea name="final[]"></textarea></td>
        <td><textarea name="act[]"></textarea></td>
    </tr>
        <td><input type='submit' name='Submit' value='Submit'></td>
    </tr>
  </table>
</form>

然后我编码mypdf.php

#mypdf.php
<?php
include('pdf.php');
    if(isset($_POST['Submit'])){
    $unit = $_POST['unit'];
    $final = $_POST['final'];
    $act = $_POST['act'];

    // get your $N value for hear
    for($i = 0; $i < 3; $i++){
        $data[] = array($unit[$i], $final[$i], $act[$i]);
    }

    $h1_heading = "Test 1";

    $pdf = new PDF('P', 'mm', 'A4');
    $pdf->AliasNbPages();
    $pdf->SetFont('Times','B',12);
    $pdf->SetTextColor(231,69,16);
    $w = $pdf->GetStringWidth($h1_heading)+40;
    $pdf->AddPage();
    $pdf->Cell($w,-12,$h1_heading, 0,1,'C');
    $pdf->Ln(25);
    //Column titles
    $header=array('Unit','Final','Act');
    $pdf->SetFont('Arial','',11);
    //Header
    // make an array for the column widths
    $w=array(25,30,25);
    // call the table creation method
    $pdf->BuildTable($header,$data,$w);
    $pdf->Output();

} else {
    header("Location:index.php");
    }
?>

这是我的pdf.php

#pdf.php
<?php 

require('fpdf.php');

class PDF extends FPDF {    

        // Page Header
        function Header() {     
            date_default_timezone_set('Asia/Colombo'); 
            $timestamp = time();            
            $this->SetX(110);
            $this->Cell(90,10,date("Y/m/d h:i:s A ",$timestamp), 0,0,'C');
            $this->Ln(30);
        }

        //Page footer method
        function Footer()       {
            //Position at 1.5 cm from bottom
            $this->SetY(-15);
            $this->SetFont('Arial','I',8);
            $this->SetTextColor(145,55,25);
            $this->Cell(0,10,'Page '
            .$this->PageNo().'/{nb}',0,0,'C');
        }       

        function BuildTable($header,$data,$w) {
            //Colors, line width and bold font
            $this->SetFillColor(255,153,51);
            $this->SetTextColor(165,42,42);
            $this->SetDrawColor(244,81,61);
            $this->SetLineWidth(.1);
            $this->SetFont('','B',8);

            // send the headers to the PDF document
            for($i=0;$i<count($header);$i++) 
                $this->Cell($w[$i],7,$header[$i],1,0,'C',1);

            $this->Ln();

        //Color and font restoration
        $this->SetFillColor(255,204,102);
        $this->SetTextColor(244,81,61);
        $this->SetFont('');



        //now spool out the data from the $data array
        $fill=true; // used to alternate row color backgrounds

        foreach($data as $row) {
            // flips from true to false and vise versa
            $fill =! $fill;
            if($fill==0){
                for($i = 0; $i<count($header); $i++ ){
                    // restore normal color settings
                    $this->SetFillColor(255,255,153);
                    $this->SetTextColor(244,81,61);
                    $this->SetFont('');
                    $this->Cell($w[$i],6,$row[$i],1,0,'C',true);
                }
                $this->Ln();
            } else {
                for($i = 0; $i<count($header); $i++ ){
                    // restore normal color settings
                    $this->SetFillColor(255,204,102);
                    $this->SetTextColor(244,81,61);
                    $this->SetFont('');
                    $this->Cell($w[$i],6,$row[$i],1,0,'C',true);
                }
                $this->Ln();
            }
        }
        $this->Cell(array_sum($w),0,'','T');
    }
}
?>

试试这个。我认为这对你有帮助。 :)