To create a pdf from a form using php. In this tutorial, you will learn how generate a pdf file using fpdf in php.
Sometimes, you need to generate a pdf file from html forms and want to store in your php app.
So, this tutorial will guide you step by step on how to create pdf file using the fpdf in php.
How to create PDF file using Fpdf in PHP
Follow following the below steps and upload file using dropzone js and jquery without refreshing the whole web page in PHP:
- Step 1 – Download fpdf Library
- Step 2 – Create index.php
- Step 3 – Create pdf.php
Step 1 – Download fpdf Library
In this step, you need to visit the following link and download the fpdf library.
http://www.fpdf.org/.
Note that, Add this library in pdf.php file.
Step 2 – Create index.php
In this step, you need to visit your project directory and create an index.php file. Then add the below HTML code into your index.php file:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Simple Registration Form in PHP - Tutsmake.com</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="row"> <div class="col-lg-8 col-offset-2"> <div class="page-header"> <h2>Registration Form in PHP</h2> </div> <p>Please fill all fields in the form</p> <form action="pdf.php" method="post"> <div class="form-group"> <label>Name</label> <input type="text" name="name" class="form-control" value="" maxlength="50" required=""> </div> <div class="form-group "> <label>Email</label> <input type="email" name="email" class="form-control" value="" maxlength="30" required=""> </div> <div class="form-group"> <label>Mobile</label> <input type="text" name="mobile" class="form-control" value="" maxlength="12" required=""> <span class="text-danger"> </div> <input type="submit" class="btn btn-primary" name="signup" value="submit"> </form> </div> </div> </div> </body> </html>
This HTML code shows the registration form.
Step 3 – Create pdf.php
In this step, create a new file name pdf.php file and add the below code into your pdf.php file:
<?php $name = $_POST['name']; $email = $_POST['email']; $mobile = $_POST['mobile']; require("fpdf/fpdf.php"); $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('arial','',12); $pdf->Cell(0,10,"Registration Details",1,1,'C'); $pdf->Cell(20,10,"Name",1,0); $pdf->Cell(45,10,"Email",1,0); $pdf->Cell(45,10,"Mobile",1,0); $pdf->Cell(20,10,$name,1,0); $pdf->Cell(45,10,$email,1,0); $pdf->Cell(45,10,$mobile,1,0); $file = time().'.pdf'; $pdf->output($file,'D'); ?>
The pdf.php file code will create pdf file using fpdf library.
Conclusion
In this tutorial, you have learned how to create PDF file in PHP using fpdf.