How to generate pdf using php

We can generate a real time pdf using php with the help of FPDF library. This library used to make the pdf file on the run time. It allows the php to write in to the pdf in cells. It generates very high quality pdf just like normally pdf files are.

You only have to make a child class of FPDF library. And then our methods will generate the pdf file accordingly. Here is the class you have to make first

<?php
require("fpdf/fpdf.php");
class PDF extends FPDF
{
// Page header
function Header()
{
    // Logo
    // Arial bold 15
    $this->SetFont('Arial','B',15);
    // Move to the right
	$this->Ln(20);
    $this->Cell(80);
    // Title
    $this->Cell(30,10,'Welcome Test user,',0,1,'C');
	$this->Ln(1);
	$this->Cell(80);
	$this->Cell(30,10,'Report 24-Nov-2017 To 30-Nov-2017',0,1,'C');
	$this->Ln(10);
	// Table headings
	$this->Cell(10,10,'#',1,0,'C');
	$this->Cell(90,10,'Section',1,0,'C');
	$this->Cell(90,10,'Total',1,1,'C');
    // Line break
    
}

// Page footer
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');
}
}

// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
// row
$pdf->Cell(10,10,'1',0,0,'C');
$pdf->Cell(90,10,'Section 1',0,0,'C');
$pdf->Cell(90,10,'2',0,1,'C');
// row
$pdf->Cell(10,10,'2',0,0,'C');
$pdf->Cell(90,10,'Section 2',0,0,'C');
$pdf->Cell(90,10,'22',0,1,'C');
// row
$pdf->Output();
?>

This is all. If you wan’t to get the ready to go code you can download the zip file below. And hit the form.php directly, It will show you directly the pdf file.

Download Now