MySQL database export to pdf using PHP

To export whole database or whole table content we can use phpMyAdmin Export option but if you want to export some rows or any query result then we should perform some other way.
To create PDF file we should first download the FPDF Library – Click Here.
Copy the following code and create a new PHP file called exportpdf.php and paste the content into it or download the file – Click Here
Sample Output

Export PDF

Sample Code

class PDF extends FPDF
function Header()
$name="Testing PDF Creation";
    //Move to the right
    $this->Cell(20,40,"Data Generated For $name on ".date('d-m-Y'),0,0,'C');
$this->Cell(10,60,"Test Place 1",0,0,'C');
$this->Cell(-10,70,"Test Place 2",0,0,'C');
    //Line break
//Page footer
function Footer()
//Load data
function LoadData($file)
//Read file lines
foreach($lines as $line)
return $data;
//Simple table
function BasicTable($header,$data)
foreach ($data as $eachResult) 
{ //width
//Better table
$pdf=new PDF();
$header=array('Name','Date','Transaction Data','Failed Trasactions','Banks Transffered');
//Data loading
//*** Load MySQL Data ***//
$objConnect = mysql_connect("localhost","databaseuser","databasepassword") or die("Error:Please check your database username & password");
$objDB = mysql_select_db("databasename");
$strSQL = "SELECT bookstall_id, name, location, address, telephone FROM bookstall";
$objQuery = mysql_query($strSQL);
$resultData = array();
for ($i=0;$i<mysql_num_rows($objQuery);$i++) {
$result = mysql_fetch_array($objQuery);
function forme()
echo "PDF generated successfully. To download document click on the link >> <a href=".$d.".pdf>DOWNLOAD</a>";
//*** Table 1 ***//

You can leave a response, or trackback from your own site.

Leave a Reply