如何在提交后以pdf和邮件发送表格数据

时间:2014-03-29 05:51:26

标签: php forms fpdf

我已经制作了一个简单的表格,它取值但现在我想制作一份提交表格数据的pdf,当我点击提交方式后,在提交表格后使用时点击提交然后所有表格值将以pdf格式形成。 (我想通过邮件发送pdf,但首先我想了解如何制作pdf然后我会在以后再这样做。)

我已经为表单编写了代码,然后我使用了fpdf的帮助,并尝试了解其类的使用。

    <?php
    require('fpdf.php');

    /*$pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->SetTextColor(50,60,100);
    $pdf->AddPage('P');
    $pdf->SetDisplayMode(real,'default');
    $pdf->Cell(40,10,'Hello World!');
    $pdf->Output();
    */

$labelname = "Membership Form" ;
 include_once 'header.php';?>

我只是将这些课程放在评论中,否则我的membership-form.php将不会显示,并且将显示空白的pdf页面。

据我所知,我必须使用addpage,cell和setfont等函数来设置pdf的样式,并且为了放置数据,我应该使用它:

function ChapterBody($file)
{
    // Read text file
    $txt = file_get_contents($file);
    // Font
    $this->SetFont('Times','',12);
    // Output text in a 6 cm width column
    $this->MultiCell(60,5,$txt);
    $this->Ln();
    // Mention
    $this->SetFont('','I');
    $this->Cell(0,5,'(end of excerpt)');
    // Go back to first column
    $this->SetCol(0);
}

这是我脑子里没有工作的部分,因为我对这段代码一无所知。请告诉我如何从表格中获取数据并输入pdf,以便在提交后以pdf格式显示。

这是我表格的一小部分

<table border="0" width="100%;">
<form action="mail_handler.php" method="post">
<tr>
<td>Title</td>
<td>First Name</td>
<td>Middle</td>
<td>Surname</td>
</tr>
<tr>
<td><input type="text" name="title" /> </td>
<td><input type="text" name="fname" /></td>
<td><input type="text" name="middle" /></td>
<td><input type="text" name="surname" /></td>
</tr>

<tr>
<td>Mailing Address</td>
</tr>
<tr>
<td><input type="text" name="email" /></td>
</tr>

<tr>
<td colspan="2">Name of Company / Institution & Address</td>
</tr>
<tr>
<td><input type="text" name="noc" width="30%"/></td>
<td><input type="text" name="noc1" width="30%"/></td>
</tr>

<tr>
<td>Office Tel.</td>
<td>Home Tel.</td>
<td>Mobile No.</td>
<td>E-mail</td>
</tr>
<tr>
<td><input type="text" name="office_no" /> </td>
<td><input type="text" name="Home_no" /></td>
<td><input type="text" name="mobile_no" /></td>
<td><input type="text" name="email" /></td>
</tr>
<tr><td a align="center" colspan="4"><input type="submit" value="submit" name="submit" /></td></tr>
</form>
</table>

我也读过这个问题Sending email filled PDF form,但它并没有解决我的问题。

2 个答案:

答案 0 :(得分:2)

经过大量的搜索和负面评价......我确实解决了我的问题..

只需编辑mypdf.php

即可

mypdf.php

<?php
include('pdf.php');

if(isset($_POST['submit']) && $_POST['submit']!='' && $_POST['fname']!='')
{
//for mail(i din use it as m wrking offline but hope it will work
/*$to = "xyz@gmail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['fname'];
    $last_name = $_POST['surname'];
    $mbile = $_POST['mobile_no'];
    $email = $_POST['email'];
    $subject = "Form submission";
    //$subject2 = "Copy of your form submission";
    $message = $first_name . " " . $last_name . " " . $mbile . " " . $email . "\n\n" . $_POST['message'];
    //$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    //mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    // You cannot use header and echo together. It's one or the other.
*/


       $msg = "title: " .$_POST['title'] . "\n"
            ."fname: " .$_POST['fname'] . "\n"
            ."middle: " .$_POST['mail_add'] . "\n"
            ."surname: " .$_POST['mobile_no'] . "\n"
            ."email: " .$_POST['email'] . "\n"
            ."mail_add: " .$_POST['education'];
        $staffEmail = "staffemail";

    // get your $N value for hear





    $h1_heading = "User Detail";
        $pdf_filename = tempnam(sys_get_temp_dir(), "pdf");
        $pname = explode("\\", $pdf_filename);
        $a=$pname[3];
        $pname1 = explode(".", $a);
        $cname=$pname1[0];//final name for file
        $pdf=new FPDF();
        $pdf->AddPage();
        $pdf->SetFont("Arial", "B", 16);
        $pdf->Cell(40, 10, "User Detail");
        $pdf->Ln();
        $pdf->SetFont("Arial", "", 12);
        $pdf->Cell(40, 10, "First Name:");
        $pdf->SetFont("Arial", "B", 12);
        $pdf->Cell(40, 10, $_POST['fname']);
        $pdf->Ln();
        $pdf->SetFont("Arial", "", 12);
        $pdf->Cell(40, 10, "Address:");
        $pdf->SetFont("Arial", "B", 12);
        $pdf->Cell(40, 10, $_POST['mail_add']);
        $pdf->Ln();
        $pdf->SetFont("Arial", "", 12);
        $pdf->Cell(40, 10, "Mobile No:");
        $pdf->SetFont("Arial", "B", 12);
        $pdf->Cell(40, 10, $_POST['mobile_no']);
        $pdf->Ln();
        $pdf->SetFont("Arial", "", 12);
        $pdf->Cell(40, 10, "E-mail:");
        $pdf->SetFont("Arial", "B", 12);
        $pdf->Cell(40, 10, $_POST['email']);
        $pdf->Ln();
        $pdf->SetFont("Arial", "", 12);
        $pdf->Cell(40, 10, "Education:");
        $pdf->SetFont("Arial", "B", 12);
        $pdf->Cell(40, 10, $_POST['education']);
        $a=$_POST['mobile_no'];
        $pdf->output("pdf/". "$a" .$cname.".pdf" , 'f');
        $pdf->Ln(45);
        header("Location:index.php");
}
else
{
header("Location:membership-form.php");
}

?>

答案 1 :(得分:0)

供参考:

您可以使用MPDF lib生成PDF。在其中,您只需传递HTML内容。

对于电子邮件发送和附件PHP Mailer