FPDF is a PHP class which enables you to create PDF records, and does not rely upon extra PHP libraries.

FPDF is free and can be downloaded from the official site’s download segment. The download bundle contains every fundamental document, alongside a few tutorial on how to use it.

You can upload your package on the SiteGround account and extract it after that. You can place the extracted contents in any folder on your account. That will at that point transform into the FPDF installation folder.

An basic example of using FPDF is with the following PHP code. You can download the FPDF from here.

Note : You should extract the FPDF package in the folder where the PHP file with the code is located.

<?php
$dbHost = 'localhost' ;
$dbUser = 'root' ;
$dbPass = '' ;
$dbName = 'multidelete' ;
$con = mysqli_connect('localhost', 'root', '') ;
if(!$con){
    die("Could not connect to the server: " .mysqli_error($con));
}

mysqli_select_db($con, 'pdf') or die('could not connect to the database' .mysqli_error($con));

$result = mysqli_query($con, "SELECT * FROM zipcode");

$header = mysqli_query($con, "SELECT UCASE(COLUMN_NAME) FROM `INFORMATION_SCHEMA`.`COLUMNS`
                                                                WHERE `TABLE_SCHEMA` = 'pdf'
                                                                AND `TABLE_NAME` = 'zipcode'
                                                                AND `COLUMN_NAME` in ('id', 'country', 'city', 'zipcode')");

    require ('fpdf181/fpdf.php');

    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial', 'B', 16);

foreach ($header as $heading){
        foreach ($heading as $column_heading) {
            $pdf->cell(35, 12, $column_heading, 1);
        }
    }

    foreach ($result as $row){
        $pdf->Ln();
        foreach ($row as $column){
            $pdf->cell(35,12,$column,1);
        }
    }

    $pdf->output();
?>

In the above code we have connected to the database and directly fetch the data from the database. As a result whenever we will run this page, we will get the pdf file. You can also set the font and design the width and height of the column cell.

CONCLUSION

In this article, we discussed how you could download PDF File using PHP. Also we will be using the FPDF library for the support. This is a simple example you can add more complex logic and validations as per your requirements. If you wish to add to the discussion or would like to ask a question, leave a comment below.

Download Code

Comments

  1. I am curious to find out what blog system you happen to be
    using? I’m having some minor security issues
    with my latest blog and I would like to find something more safeguarded.
    Do you have any suggestions?

  2. Pingback: Google

Leave a Reply

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