Code Test Generate PDF with php fpdf

by - June 18, 2014


FPDF is a PHP class which allows to generate PDF files with pure PHP.

Design and generate PDF. Reservation statement


Sample code : 

<?php 

require('fpdf.php');
include("../connection.php");

$reservation_id = $_REQUEST['reservation_id'];
$member_id = $_REQUEST['member_id'];

$sql = "select member_name from member where member_id = $member_id";
$result_member = mysql_query($sql);
$row_member = mysql_fetch_assoc($result_member);
$member_name = $row_member['member_name'];

$sql = "select * from reservation join homestay on reservation.homestay_id = homestay.homestay_id ".
        "join member on homestay.member_id = member.member_id where reservation.reservation_id = $reservation_id";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);

$start = $row['checkIn_date'];
$end = $row['checkOut_date'];
$reservation_date = $row['reservation_date'];
$night = (strtotime($end)- strtotime($start))/24/3600; 

class PDF extends FPDF
{
// Page header
function Header()
{
    // Logo
    $this->Image('../images/homestay-logo.png',10,10,50);   
    // Move to the right
    $this->Cell(100);
    // Arial bold 15
    $this->SetFont('Arial','B',20);
    // Title
    $this->Cell(0,20,'Statement of Reservation','C');
    // Line break
    $this->Ln(20);
}

// 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->AddPage();

$pdf->SetFont('Times','B',18);
$pdf->Cell(80,10,'MyHomestay');
$pdf->Ln();
$pdf->SetFont('Times','',11);
$pdf->Cell(80,0,'1234 ST.');
$pdf->Ln();
$pdf->SetFont('Times','',11);
$pdf->Cell(80,7.5,'Denver, CO 80202');
$pdf->Ln();
$pdf->SetFont('Times','',11);
$pdf->Cell(80,0,'Phone:(60)123-4567');
$pdf->Ln();
$pdf->SetFont('Times','',11);
$pdf->Cell(80,7.5,'myhomestaymmu@gmail.com');
$pdf->Ln();
$pdf->SetFont('Times','',11);
$pdf->Cell(80,0,'www.homestay.com');
$pdf->Ln();
$pdf->Cell(130);
$pdf->SetFont('Arial','',12);
$reservation_date = date("Y-m-d",strtotime($reservation_date));
$pdf->Cell(0,-40,'Reservation Date :  '.$reservation_date,'C');
$pdf->Cell(0,-40,'','C');

$date = date("Y-m-d");
$pdf->Cell(-53.5,-25,'Generate Date :  '.$date,0,1,'C');

$pdf->Cell(0,40,'',0,1,'R');



$word = "To,".
        "\n$member_name".
        "\nDear Sir/Madam,".
        "\nThank you for your reservation. We confirm with pleasure your accommodation.".
        "\nPlease note the information below for accuracy".
        "\nPlease review it and report any discrepancies.";
$pdf->Multicell(160,6,$word);

$pdf->Cell(0,10,'',0,1,'R');

$pdf->SetFillColor(169, 169, 169);
$pdf->SetTextColor(0);
$pdf->SetDrawColor(255, 255, 255);
$pdf->Cell(18,7,'Res.Id',0,0,'L',true);
$pdf->Cell(35,7,'Guest Name',0,0,'L',true);
$pdf->Cell(35,7,'Homestay Name',0,0,'L',true);
$pdf->Cell(28,7,'Check In',0,0,'C',true);
$pdf->Cell(28,7,'Check Out',0,0,'C',true);
$pdf->Cell(18,7,'Nights',0,0,'C',true);
$pdf->Cell(26,7,'Amount',0,1,'C',true);

$pdf->Cell(0,0,'',0,1,'R');
$pdf->Cell(15,7,$row['reservation_id'],1,0,'C',0);
$pdf->Cell(28,7,$member_name,1,0,'C',0);
$pdf->Cell(44,7,$row['homestay_name'],1,0,'C',0);
$pdf->Cell(28,7,$row['checkIn_date'],1,0,'C',0);
$pdf->Cell(30,7,$row['checkOut_date'],1,0,'C',0);
$pdf->Cell(17,7,$night,1,0,'C',0);
$pdf->Cell(25,7,$row['reservation_total_price'],1,0,'C',0);

// Linienfarbe auf Blau einstellen 
$pdf->SetDrawColor(0, 0, 0);

// Linienbreite einstellen, 0.5 mm
$pdf->SetLineWidth(0.5);

// Linie zeichnen
$pdf->Line(10,132,198,132);

$pdf->SetFont('Arial','B',12);

$reservation_total_price = $row['reservation_total_price'];

$pdf->Cell(0,18,'',0,1,'R');
$pdf->Cell(182,3,'Total:  RM'.$reservation_total_price,0,1,'R');
$pdf->Cell(0,3,'',0,1,'R');


$pdf->Output();

?>

You May Also Like

0 comments