Create PDF in PHP

Introduction

In this article I will explain how to display data in a PDF file using PHP. To creating the PDF file, I will first includ the "fpdf.php" file of your pdf.php. This file you can download free for creating PDF files. Mostly for creating PDF and display the data in a PDF file so this file fpdf.php and the font requirements of your new PDF file. The font folder includes many types of fonts and this folder is put in the same directory, "www".

Example

This is a "connection.php" for creating a connection to your table in a MySQL database:

<?php

$con = mysql_connect("localhost","root","");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("smart", $con);

 

mysql_query("SELECT * From form");

 

?>


This is a "pdf.php" file for creating a PDF page and in this file, I have included a connection.php file and fpdf.php file, both files are saved in the same directory. I am using this script for creating a PDF and displaying the data from your table. I have used our table and displayed some data. Such as display Id, name, mobile, email in PDF on our browser.
Mysql table.jpg
<?php
include
"connection.php";
include('fpdf.php');
$
pdf=new FPDF();

//Creating new pdf page

$pdf->AddPage();
//Set the base font & size
$pdf->SetFont('Arial','B',10);
$pdf->Cell(100,5,"Create PDF File In PHP");
//Creating new line
$pdf->Ln();
$pdf->Ln();
$pdf->SetFont('Arial','B',6);
$pdf->Cell(10,5,"[Sr.No.]");
$pdf->Cell(30,5,"[name]");
$pdf->Cell(30,5,"[Mobile]");
$pdf->Cell(25,5,"[email]");
$pdf->Ln();
$pdf->Cell(350,5,"---------------------------------------------------------------------------------------------------------------------");

// Fetch data from table

 $result=mysql_query("select * from form");

 while($row=mysql_fetch_array($result))

 {

  $pdf->Cell(10,5,"{$row['Id']}");

  $pdf->Cell(30,5,"{$row['name']}");

  $pdf->Cell(30,5,"{$row['Mobile']}");

  $pdf->multiCell(25,5,"{$row['email']}");

 }

$pdf->Output();

?>

Output
Display Pdf in PHP.jpg


Similar Articles