Search code examples
phpmysqlhtmlemailfpdf

How to use html form data for fpdf form creation


i am trying to create a pdf email using data inputed into a html form. I don't know how to use data from a mysql database or html form to create a pdf using fpdf and email it. I am close to finishing this project i have been working on and need help closing off the deal i have a difficult html form using specially spaced tables and such trying to reproduce a excel document to a html form doing javascript calculations and now all i need to do is email this form in a pdf format using php or something using my linux webserver. please i really want to know this for myself as well. i have learned alot in the past few weeks about html,php,mysql, and javascript and i started this as a project for fun and now people want it. Sorry. And thank you all for all your help for teaching me this information. ok heres the code.

  <!DOCTYPE html>
  <html>
  <head>
 <?php
 $intro = mysql_real_escape_string($_POST['intro']);
 $hostname ="localhost";
 $db_user = "someuser";
 $db_password = "somepassword";
 $database = "form_something";
 $db_table = "FORM";
 $db = mysql_connect ($hostname, $db_user, $db_password);
 mysql_select_db($database,$db);
 ?>
  <title><h2>this is my form</h2><title>
  <?php
  if (isset($_REQUEST['Submit'])) { 
  # THIS CODE TELLS MYSQL TO INSERT THE DATA FROM THE FORM INTO MY MYSQL TABLE 
  $sql = "INSERT INTO $db_table(information) values ('".mysql_real_escape_string(stripslashes($_REQUEST['information']))."')";
  if($result = mysql_query($sql ,$db)) { 
  echo <h1>Thank you</h1>information has been entered into our database<br><img   src="http://someimage.com/here/is/the/image.jpg"';
 {
 p
 } else 
 { 
 echo "ERROR: ".mysql_error(); 
 }
 } else 
 {
 ?>
  ?>
  </head>
  <form action="pdf.php">
  <td colspan="2">type your information here<input type="text" name="information">
  <input type="submit" name="Submit" value="Submit">
  </body>
  </form>
  </html>

and the basic fpdf page pdf.php looks like this

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

class PDF extends FPDF
 {
 // Page header
function Header()
{
// Logo
$this->Image('logo.png',140,6,60);
// Arial bold 15
$this->SetFont('Arial','B',15);
// Move to the right
$this->Cell(60);
 // Title
$this->Cell(80,10,' Form',1,0,'C');
// Line break
$this->Ln(20);
}

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

$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
for($i=1;$i<=40;$i++)
$pdf->Cell(0,10,'Printing line number '.$i,0,1);
$pdf->Output();
?>

Solution

  • I found it to be pretty intuitive linking a HTML form with FPDF. Make your form in HTML, in this example I'm creating a Text Field and an Option Menu. Note that i create the form with the tag action="to_pdf.php" which will link the form values with php.

    <form action="to_pdf.php" method="POST">
        <p>Name</p>
        <input type="text" name="name">
    
        <p>Priority</p>
        <select name="priority" size="1">
            <option value="Low">Low</option>
            <option value="Normal">Normal</option>
            <option value="High">High</option>
            <option value="Emergency">Emergency</option>
        </select>
    
        <input type="submit" value="Send"><input type="reset" value="Clear">
    </form>
    

    Then import the form values into your php file, the one that we already stated on the HTML, to_pdf.php

    <?php
    
    $name = $_POST['name'];
    $priority = $_POST['priority'];
    
    require('fpdf.php');
    
    $pdf = new FPDF();
    $pdf->AddPage();
    
    $pdf->SetFont('Arial','B',16);
    
    $pdf->MultiCell(40,10,$name);
    $pdf->MultiCell(40,10,$priority);
    
    $pdf->Output(); 
    
    ?>
    

    After that, you only need to make sure that you have the fpdf.php file in the path you stated. And remember to run your test on a web server or on localhost so the php files actually run.