• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Tutsplanet

Tutsplanet

Free Technical and Blogging Resources

  • Home
  • Web Hosting
  • Programming
  • Plugins
  • Twitter Trends
  • Tools
  • About Us

Programming TagsPHP

How to Create PDF File From HTML Form Using Fpdf?

Aug 22, 2021 Editorial Staff 1 Comment

Share
Tweet
Share
5 Shares

In today’s tutorial, we illustrate how to convert HTML to pdf with PHP.  As you know already, PDF stands for “portable document format”, a format that is used when we need to save a file that cannot modify but can be printed easily.

In today’s article, we will show you have to post a form and save the values into pdf in PHP. So, this tutorial will guide you step by step on how to create pdf file using the fpdf in PHP.

How to create a PDF file using Fpdf in PHP

Follow following the below steps and upload the 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 convert-pdf.php

So we dig in to the real steps of implementation in the tutorial

Step 1: Download fpdf Library

Download the fpdf library from this URL http://www.fpdf.org/ and extract the contents to the vendor folder.

Step 2: Create index.php

Your index.php will look like below with a form having several basic fields to capture the basic user details. You already know that we use the Twitter bootstrap library for the styling of the page, which makes our styling easy.

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">

    <title>Convert HTML to PDF using FPDF</title>
  </head>
  <body>
    <div class="container">
        <div class="row">
            <div class="col-lg-7 mx-auto">
                <h5 class="text-center mt-5 mb-5">Convert HTML to Pdf using Fpdf</h5>
                <form action="convert-pdf.php" method="post">
                <div class="mb-3">
                <label for="name" class="form-label">Name</label>
                <input type="text" class="form-control" id="name" name="name">
                </div>
                <div class="mb-3">
                <label for="email" class="form-label">Email</label>
                <input type="text" class="form-control" id="email" name="email">
                </div>
                <div class="mb-3">
                <label for="message" class="form-label">Message</label>
                <input type="text" class="form-control" id="message" name="message">
                </div>
                <div class="mb-3">
                <button type="submit" class="btn btn-primary mb-3">Submit</button>
                </div>
                </form>
            </div>
            
        </div>        
    </div>

  </body>
</html>

Step 3: Create convert-pdf.php

This page has a vital role in our application, as it receives the values from the form and processes them into a pdf file. In the first section, we include the required fpdf library from the vendor folder. And then we receive the post variables from the form and pass this to the fpdf class which prints these values to the pdf.

<?php
#includes the fpdf main libarary
require_once("vendor/fpdf/fpdf.php");

#recieving the form data
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
    
 
$pdf = new FPDF();
$pdf->AddPage();

$pdf->SetFont('arial','',12);
$pdf->Cell(0,10,"Contact Details",1,1,'C');
$pdf->Cell(40,10,"Name",1,0);
$pdf->Cell(75,10,"Email",1,0);
$pdf->Cell(75,10,"Message",1,0);
$pdf->Ln();

$pdf->Cell(40,10,$name,1,0);
$pdf->Cell(75,10,$email,1,0);
$pdf->Cell(75,10,$message,1,0);

$file = time().'.pdf';
$pdf->output($file,'D');
?>

That’s it, once everything working smoothly you will get your pdf files something like the below image.

How to Create PDF File From HTML Form Using Fpdf?


Editorial Staff

Editorial Staff at Tutsplanet is a dedicated team to write various tutorials about subjects like Programming, Technology and Operating Systems.

View all posts by Editorial Staff

Reader Interactions

Comments

  1. Patrick C says

    Apr 21, 2022 at 5:28 pm

    How would I add a image to this?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Quick Links

  • Top 21 Website Ideas To Make Money Online in 2021
  • A Simple YouTube Video Downloader Script in PHP
  • The 50 Most Useful jQuery Plugins for Frontend Development
  • Replace “\n” with new line characters, using Notepad++
  • Using Third-Party Libraries in Codeigniter
  • Upload Multiple Images and Store in Database using PHP and MySQL.
  • Hierarchical Tree view Category Example in Laravel
  • Laravel Image Intervention Tutorial With Example
  • How to import sql file in MySQL database using PHP?
  • Free VAT Calculator Online

Subscribe

* indicates required



Search Here

Share

   



Hot topics

  • Replace “\n” with new line characters, using Notepad++ 14 views
  • Solution: windows photo viewer opens each time save a pic file 12 views
  • Simple PHP Shopping Cart 10 views
  • A Simple YouTube Video Downloader Script in PHP 10 views
  • How to Create PDF File From HTML Form Using Fpdf? 9 views
  • All Countries SQL file with Country Codes to Download 8 views
  • How to create and download a Zip file with PHP 8 views
  • How to set up GitHub SSH in Ubuntu with example 7 views
  • How to enter new line in Microsoft Teams? 7 views
  • Laravel Eloquent Select Column as Alias 7 views

Categories

  • Design & Development
  • Drupal
  • Facebook
  • General
  • How To
  • ios
  • Javascript
  • Linux
  • Magento
  • Marketing
  • News
  • PHP
  • Plugins
  • Programming
  • Snippets List
  • Social Media
  • Softwares
  • Themes
  • Tips
  • Wordpress
  • YouTube























Copyright © 2023 · Planet on Genesis Framework · Powered By BunnyCDN . Network wallpapernoon.com