How to create PDF file on form submit

Project : in this project we will learn how to create pdf file and save in invoices folder.

FPDF Library : We have to add fpdf library in this project to generate pdf file.

fpdf folder that is pdf genertor from fpdf.org which is free source library.

in this project we will use fpdf generator library to generate pdf file which is free source library.

You can download fpdf library, the link is given below.

Download FPDF Generator.


invoices (folder)

In this folder all the pdf file will be saved inside invoices folder after form submit
PHP
HTML FORM on (index.php)
PHP

index.php

myproject/index.php
index.php
                            
                              <?php include("generate.php"); ?>
                                <!DOCTYPE html>
                                <html>
                                <head>
                                  <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
                                  <title>Codetechinfo </title>
                                </head>
                                <body>
                                  <div class="container w-75"><br>
                                  <div><?php echo $error; ?> <?php echo $msg; ?></div>
                                    <form method="POST" action="index.php"  class="border w-75 p-3" style="background: #f5f6fa;">
                                        <h4 class="rounded bg-primary w-100 p-2 text-white text-center">Generate PDF File</h4>
                                          <div class="form-group row">
                                              <div class="col-md-6">
                                                  <label for="order_date">Date</label>
                                                  <input type="text" class="form-control" value="<?php echo date('d-m-yy'); ?>" name="order_date" id="order_date" Readonly>
                                              </div>
                                              <div class="col-md-6">
                                                  <label for="name">Customer Name</label>
                                                  <input type="text" class="form-control"  name="name" id="name">
                                              </div>
                                          </div>
                                            <div class="form-group row">
                                              <div class="col-md-6">
                                                  <label for="address">Address</label>
                                                  <input type="text" class="form-control"  name="address" id="address">
                                              </div>
                                              <div class="col-md-6">
                                                  <label for="product_name">Product Name</label>
                                                  <input type="text" class="form-control"  name="product_name" id="product_name">
                                              </div>
                                            </div>
                                            <div class="form-group row">
                                              <div class="col-md-6">
                                                  <label for="quantity">Quantity</label>
                                                  <input type="text" class="form-control" name="quantity" id="quantity">
                                              </div>
                                              <div class="col-md-6">
                                                  <label for="price">Product Price</label>
                                                  <input type="text" class="form-control"  name="price" id="price">
                                              </div>
                                            </div>
                                            <div class="form-group row">
                                              <div class="col-md-6">
                                                  <label for="gst">GST Rate(%)</label>
                                                  <input type="text" class="form-control" name="gst" id="gst">
                                              </div>
                                              <div class="col-md-6">
                                                  <label for="discount">Discount</label>
                                                  <input type="text" class="form-control" name="discount" id="discount">
                                              </div>
                                            </div>
                                            <div class="form-group row">
                                              <div class="col-md-6">
                                                  <label for="paid">Paid Amount</label>
                                                  <input type="text" class="form-control" name="paid" id="paid">
                                              </div>
                                              <div class="col-md-6">
                                                  <label for="method">Payment Method</label>
                                                  <select name="method" id="method" class="form-control">
                                                     <option value="Cash">Cash</option>
                                                     <option value="Debit Card">Debit Card</option>
                                                     <option value="Credit Card">Credit Card</option>
                                                     <option value="Cheque">Cheque</option>
                                                     <option value="Bank Transfer">Bank Transfer</option>
                                                  </select>
                                              </div>
                                            </div>
                                            <div class="form-group text-white pb-2">
                                              <input type="submit" name="submit" value="Generate PDF" class="btn btn-success"> 
                                              <a href="index.php" class="btn btn-danger float-right">Close</a>
                                            </div>
                                        </form>
                                  </div><br>
                                </body>
                                </html>
                            
                          

Generate PDF

myproject/generate.php
generate.php
                              
                                <?php
                                    include("fpdf/fpdf.php"); // include php library file
                                    if(isset($_POST['submit'])){
                                      if(!empty($_POST['name']) || !empty($_POST['price']) || !empty($_POST['quantity']) || !empty($_POST['amount'])){
                                        $pdf = new FPDF();
                                        $pdf->AddPage();
                                        $pdf->Rect(5, 5, 200, 287, 'D'); //For A4
                                        $pdf->SetFont("Arial","B", 16);
                                        $pdf->Cell(65,15,ucwords($_POST['name']),1,1,"C");
                                        $pdf->SetFont("Arial",null,12);
                                        $pdf->SetFont("Arial","B", 12);
                                        $pdf->Cell(35,8,"Customer Name: ",0,0);
                                        $pdf->SetFont("Arial","", 12);
                                        $pdf->Cell(52,8, ucwords($_POST['name']),0,1);
                                        $pdf->Cell(35,8,"Address: ".ucwords($_POST['address']), 0, 1);
                                        $pdf->SetY(25);
                                        $pdf->Cell(168,9,"Order Date  :",0,0,"R");
                                        $pdf->Cell(23,9,$_POST['order_date'],0,1,"R");

                                        $pdf->SetY(49);
                                        $pdf->SetFont("Arial","B", 12);
                                        $pdf->Cell(50,8,"",0,1);
                                        $pdf->Cell(10,8," S.N. ",1,0,"C");
                                        $pdf->Cell(100,8,"Services/Product Name",1,0,"C");
                                        $pdf->Cell(25,8,"Quantity",1,0,"C");
                                        $pdf->Cell(25,8,"Price (Rs)",1,0,"C");
                                        $pdf->Cell(30,8,"Total (Rs)",1,1,"C");
                                        $pdf->SetFont("Arial","", 12);

                                          $pdf->Cell(10,7,'1',1,0,"C");
                                          $pdf->Cell(100,7,ucwords($_POST['product_name']),1,0,"L");
                                          $pdf->Cell(25,7, $_POST['quantity'],1,0,"C");
                                          $pdf->Cell(25,7, $_POST['price'].".00",1,0,"R");
                                          $pdf->Cell(30,7, $_POST['price']*$_POST['quantity'].".00",1,1,"R");

                                        $pdf->SetY(65); 
                                        $pdf->Cell(160,145," ",1,0);
                                        $pdf->Cell(30,145," ",1,0);
                                        $pdf->SetY(202);
                                        $pdf->SetFont("Arial","B", 12);
                                        $pdf->Cell(160,8,"Sub Total",1,0,"R");
                                        $pdf->Cell(30,8,$_POST['price']*$_POST['quantity'].".00",1,0,"R");
                                        
                                        $pdf->SetY(210);
                                        $pdf->Cell(110);
                                        $pdf->SetFont("Arial","", 12);
                                        $pdf->Cell(50,8,"GST Tax ",1,0,"R");
                                        $pdf->SetFont("Arial","B", 12);
                                        $pdf->Cell(30,8,$_POST['gst'].".00 %",1,1,"R");
                                        $pdf->Cell(110);
                                        $pdf->SetFont("Arial","", 12);
                                        $pdf->Cell(50,8,"Discount ",1,0,"R");
                                        $pdf->SetFont("Arial","B", 12);
                                        $pdf->Cell(30,8,$_POST['discount'].".00",1,1,"R");
                                        $pdf->Cell(110);
                                        $pdf->SetFont("Arial","", 12);
                                        $pdf->Cell(50,8,"Paid Amount ",1,0,"R");
                                        $pdf->SetFont("Arial","B", 12);
                                        $pdf->Cell(30,8,$_POST['paid'].".00",1,1,"R");
                                        $pdf->Cell(110);
                                        $pdf->SetFont("Arial","", 12);
                                        $pdf->Cell(50,8,"Payment Method ",1,0,"R");
                                        $pdf->SetFont("Arial","B", 12);
                                        $pdf->Cell(30,8,$_POST['method'],1,1,"R");
                                        $pdf->Output("invoices/".$_POST['name'].".pdf","F");
                                        //$pdf->Output(); 
                                        header('location: invoices/'.$_POST['name'].".pdf","F");
                                        $msg= "<div class='alert alert-success w-100'>The pdf file has been created </div>";
                                    }else{
                                        $error= "<div class='alert alert-danger w-100'>Failed to generate pdf ! something went wrong</div>";
                                    }
                                  }
                              
                          
Generated PDF File

Check the invoices folder, in this folder pdf file will be saved

PHP


css folder

You must download and include bootstrap css source files for good interface OR You can add cdn link from bootstrap website.
myproject/css
bootstrap.min.css