How to Generate PDF from MySQL Data using PHP
In this tutorial I explains how to generate PDF from MySQL Data using PHP. You are going to see how to convert MySQL Data into PDF using FPDF library.
What id FPDF ?
FPDF is a PHP class which allows to generate PDF files with PHP, that is to say without using the PDFlib library. F from FPDF stands for Free: you may use it for any kind of usage and modify it to suit your needs. FPDF has other advantages: high level functions. Here is a list of its main features:
- Choice of measure unit, page format and margins
- Page header and footer management
- Automatic page break
- Automatic line break and text justification
- Image support (JPEG, PNG and GIF)
- Colors
- Links
- TrueType, Type1 and encoding support
- Page compression
The following snippet show a basic PDF generated with FPDF :
<?php require('fpdf/fpdf.php'); $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial','B',12); $pdf->Cell(46,12,$column_heading,1); $pdf->Output(); ?>
First a db with empadata. Under db empdata create table tblemployee- Structure of tblemployee
CREATE TABLE IF NOT EXISTS `tblemployee` ( `EmpId` int(11) NOT NULL, `EmpName` varchar(100) DEFAULT NULL, `EmpDepartment` varchar(100) DEFAULT NULL, `EmpRegDate` timestamp NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1; INSERT INTO `tblemployee` (`EmpId`, `EmpName`, `EmpDepartment`, `EmpRegDate`) VALUES (1, 'Anuj kumar', 'Development', '2017-06-08 01:47:41'), (2, 'Amit ', 'HR', '2017-07-02 09:47:41'), (3, 'Garima', 'Accountant', '2016-10-14 12:48:31'), (4, 'John', 'Designing', '2017-07-01 09:48:31'), (5, 'Rahul', 'Support', '2017-06-14 09:49:00'), (6, 'Sanjeev', 'Operations', '2017-07-01 04:49:00'); ALTER TABLE `tblemployee` ADD PRIMARY KEY (`EmpId`); ALTER TABLE `tblemployee` MODIFY `EmpId` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=7;
genratepdf.php page
<?php require_once("config.php"); require('fpdf/fpdf.php'); $pdf = new FPDF(); $pdf->AddPage(); // code for print Heading of tables $pdf->SetFont('Arial','B',12); $ret ="SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='empdata' AND `TABLE_NAME`='tblemployee'"; $query1 = $dbh -> prepare($ret); $query1->execute(); $header=$query1->fetchAll(PDO::FETCH_OBJ); $cnt=1; if($query1->rowCount() > 0) { foreach($header as $heading) { foreach($heading as $column_heading) $pdf->Cell(46,12,$column_heading,1); }} //code for print data $sql = "SELECT * from tblemployee "; $query = $dbh -> prepare($sql); $query->execute(); $results=$query->fetchAll(PDO::FETCH_OBJ); $cnt=1; if($query->rowCount() > 0) { foreach($results as $row) { $pdf->SetFont('Arial','',12); $pdf->Ln(); foreach($row as $column) $pdf->Cell(46,12,$column,1); } } $pdf->Output(); ?>
please, how can i get the fpdf file because i tried to download but when i load it gives me errors.
Visit this tutorial http://www.fpdf.org/
Sir i am from pakistan .! your all stuffs are very helpful, Sir in my FYP(final year project) there is also need to generate pdf form, sir in this there is table,, is there is any source code to generate pdf form (which contains input names, fname, geneder etc) ? Thanks .! will be wait for your response
Sir email verification ka code mil sakta hy . .
i am a student of BS Computer Science final semester, i need this for final year project..Thank you sir
For email, verification visit this tutorials : https://phpgurukul.com/php-email-verification-script/
You can find the projects here: https://phpgurukul.com/php-projects-free-downloads/
Sir yi bahut helpful hy . .Thank you so much
Want to use this but shouldn’t be using PDO…Is there an alternative?
NO
i want source code, not able to download
Download link given above you can download from there.
Hi sir,
link unvailable 404 page not found
Download link is fixed. Now you can download.
Thanks you so much
where is it to download?
Download link is fixed. You can download the script.
thank you, sir.
page not found..
Check now.
perfect, thank you. looking forward to learn more from you
Hi Mr. Anuj Kumar, is there any ways that i can contact you often for asking about php?
You can contact at phpgurukulofficial@gmail.com
do i have to create a schema first
yes
thanks
hi….thanks u the code is working…but how to change a particular cell width in this…
make changes in this line $pdf->Cell(46,12,$column_heading,1);
Hi anuj,Thank you for ur reply… while changing with $pdf->Cell(46,12,$column_heading,1); all the cells width is increased …i need to increase the cell width of the last cell alone … any suggestions
how to make page landscape
Visit this tutorial—https://phpgurukul.com/how-to-convert-html-to-pdf-using-dompdf-in-php/
hi….thanks u the code is working…but how to change a particular cell width in this…
make changes in this line $pdf->Cell(46,12,$column_heading,1);
Hi anuj,Thank you for ur reply… while changing with $pdf->Cell(46,12,$column_heading,1); all the cells width is increased …i need to increase the cell width of the last cell alone … any suggestions
do i have to create a schema first
yes
how to make page landscape
Visit this tutorial—https://phpgurukul.com/how-to-convert-html-to-pdf-using-dompdf-in-php/
thanks