how to generate pdf file in php with demo?

how to generate pdf file in php with demo – The FPDF is very awesome PHP class to generate PDF using PHP from MySQL database.

how to generate pdf file in php with demo?

Let’s generate a simple pdf file, Download the latest zip file from the fpdf official site( click here ).

Generate PDF using PHP from Mysql database

  • Step-1: First of all the data from MySQL database into the web page
  • Step-2: free Download the open source FPDF library from fpdf.org
  • Step-3: Copy the fpdf.php file into your web application directory
  • Step-4: Use the fpdf PHP library like therefor

members.sql

--
-- Table structure for table `members`
--

CREATE TABLE IF NOT EXISTS `members` (
  `name` varchar(255) DEFAULT NULL,
  `designation` varchar(255) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

--
-- Dumping data for table `members`
--

INSERT INTO `members` (`name`,`designation`) VALUES
('Ravi Shastri', 'Batsmen'),
('Rohit Sharma', 'Batsmen'),
('Virat Kohali', 'Batsmen'),
('K.L Rahul', '	Batsmen'),
('Mahendrashinh Dhoni', '	Wickekeepr'),
('Sachin Tendulkar', '	Batsment'),
('Virendra Shevag', 'Batsment Opern');

index.php

runQuery("SELECT name,designation FROM members");
$header = $link->runQuery("SELECT UCASE(`COLUMN_NAME`) 
FROM `INFORMATION_SCHEMA`.`COLUMNS` 
WHERE `TABLE_SCHEMA`='pakainfo_v1' 
AND `TABLE_NAME`='members'
and `COLUMN_NAME` in ('name','designation')");

require('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);

foreach($header as $heading) {
	foreach($heading as $column_heading)
		$pdf->Cell(95,12,$column_heading,1);
}
foreach($result as $row) {
	$pdf->Ln();
	foreach($row as $column)
		$pdf->Cell(95,12,$column,1);
}
$pdf->Output();
?>

link.php

host,$this->user,$this->password,$this->database);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
      while($row = $result->fetch_assoc()) {
          $resultset[] = $row;
      }
    }
    $conn->close();

		if(!empty($resultset))
			return $resultset;
	}
}
?>

Don’t Miss : how to generate pdf file from dynamic data coming from mysql database in php?

I hope you get an idea about how to generate pdf file in php with demo.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment