【问题标题】:display form content as pdf using fpdf使用 fpdf 将表单内容显示为 pdf
【发布时间】:2012-11-15 10:10:32
【问题描述】:

我有一个回显的表单:

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();
?>

请问有人可以帮忙吗?谢谢

【问题讨论】:

    标签: php fpdf


    【解决方案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]));
    }
    

    【讨论】:

    • 给我一点时间。我试着告诉你.. :)
    • 它一直给我这个:“致命错误:调用未定义的函数:buildtable()”我已经定义了函数
    【解决方案2】:

    我试试这个。但略有不同。但我认为你可以从这段代码中找到解决方案。

    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');
        }
    }
    ?>
    

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

    【讨论】:

    • 复制了您的确切代码,但它给出了错误:“致命错误:调用未定义的函数:buildtable()”,即使该函数已定义
    • 它对我有用.. 也复制我的 pdf 课程。因为我改变了一些数据。
    猜你喜欢
    • 2016-12-22
    • 1970-01-01
    • 2019-04-19
    • 2023-04-02
    • 1970-01-01
    • 2011-04-09
    • 2023-03-20
    • 2015-06-06
    • 2014-06-20
    相关资源
    最近更新 更多