In this blog we will learn or setup live demo example to generate or download PDF invoice file by using PHP. We also upload that PDF invoice file on server by using PHP with MySQL. We are using TCPDF library for generating PDF programmatically.
TCPDF is now one of the world’s most active Open Source projects, used daily by millions of users and included in thousands of CMS and Web applications.
Now let’s start the step by step code configuration and database setup for generating PDF dynamically.
Step 1: Go to phpmyadmin and Execute below SQL statements on your MySQL database.
CREATE TABLE `INVOICE_MST` ( `MST_ID` int(11) NOT NULL AUTO_INCREMENT, `INV_NO` varchar(100) DEFAULT NULL, `CUSTOMER_NAME` varchar(255) DEFAULT NULL, `CUSTOMER_MOBILENO` varchar(10) DEFAULT NULL, `ADDRESS` text DEFAULT NULL, PRIMARY KEY (`MST_ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; insert into `INVOICE_MST`(`MST_ID`,`INV_NO`,`CUSTOMER_NAME`,`CUSTOMER_MOBILENO`,`ADDRESS`) values (1,'INV01','DIVYESH PATEL','9825121212','VALSAD, GUJARAT'); insert into `INVOICE_MST`(`MST_ID`,`INV_NO`,`CUSTOMER_NAME`,`CUSTOMER_MOBILENO`,`ADDRESS`) values (2,'INV02','ROSHNI PATEL','8457878878','CHIKHLI, GUJARAT'); insert into `INVOICE_MST`(`MST_ID`,`INV_NO`,`CUSTOMER_NAME`,`CUSTOMER_MOBILENO`,`ADDRESS`) values (3,'INV03','DITYA PATEL','7487878788','JESPOR, VALSAD'); CREATE TABLE `INVOICE_DET` ( `DET_ID` int(11) NOT NULL AUTO_INCREMENT, `MST_ID` int(11) DEFAULT NULL, `PRODUCT_NAME` varchar(255) DEFAULT NULL, `AMOUNT` double(11,2) DEFAULT NULL, PRIMARY KEY (`DET_ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; insert into `INVOICE_DET`(`DET_ID`,`MST_ID`,`PRODUCT_NAME`,`AMOUNT`) values (1,1,'DOMAIN WWW.SHINERWEB.COM',899); insert into `INVOICE_DET`(`DET_ID`,`MST_ID`,`PRODUCT_NAME`,`AMOUNT`) values (2,1,'DATABASE MYSQL',3499); insert into `INVOICE_DET`(`DET_ID`,`MST_ID`,`PRODUCT_NAME`,`AMOUNT`) values (3,1,'EMAIL SERVICES',799); insert into `INVOICE_DET`(`DET_ID`,`MST_ID`,`PRODUCT_NAME`,`AMOUNT`) values (4,2,'DOMAIN WWW.GOOGLE.COM',4500); insert into `INVOICE_DET`(`DET_ID`,`MST_ID`,`PRODUCT_NAME`,`AMOUNT`) values (5,2,'EMAIL SERVICES',799); insert into `INVOICE_DET`(`DET_ID`,`MST_ID`,`PRODUCT_NAME`,`AMOUNT`) values (6,3,'DOMAIN WWW.FACEBOOK.COM',7999);
Above sample data will be used for making invoice PDF.
Step 2: Make MySQL database connection file like database_connection.php which is used to get data from the database.
Step 3: Make one index.php where we will display our data as below.
How to generate PDF in PHP dynamically by using TCPDF
How to generate PDF in PHP dynamically by using TCPDF
Invoice # Customer name Contact # Address Action 0) < while($data_row = mysqli_fetch_array($results, MYSQLI_ASSOC)) < ?> &ACTION=VIEW" > View PDF &ACTION=DOWNLOAD" > Download PDF &ACTION=UPLOAD" > Upload PDF > ?>
Developed by Shinerweb
In above code we included font-awesome.min.css, bootstrap.min.css, jquery.min.js and bootstrap.min.js which is used for webpage design and some icons setup.
Inside tag we dynamically display invoice data from the database and that data will be use for further code.
Step 4: Now finally create one file pdf_maker.php and inside this file we will write all the logic for generate PDF. Also will write code for download and upload PDF dynamically with below PHP code.
0) < $inv_mst_data_row = mysqli_fetch_array($inv_mst_results, MYSQLI_ASSOC); //----- Code for generate pdf $pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->SetCreator(PDF_CREATOR); //$pdf->SetTitle("Export HTML Table data to PDF using TCPDF in PHP"); $pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING); $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); $pdf->SetDefaultMonospacedFont('helvetica'); $pdf->SetFooterMargin(PDF_MARGIN_FOOTER); $pdf->SetMargins(PDF_MARGIN_LEFT, '5', PDF_MARGIN_RIGHT); $pdf->setPrintHeader(false); $pdf->setPrintFooter(false); $pdf->SetAutoPageBreak(TRUE, 10); $pdf->SetFont('helvetica', '', 12); $pdf->AddPage(); //default A4 //$pdf->AddPage('P','A5'); //when you require custome page size $content = ''; $content .= ' body SHINERWEB TECHNOLOGIES CONTACT: +91 97979 97979 WEBSITE: WWW.SHINERWEB.COM CUST.NAME: '.$inv_mst_data_row['CUSTOMER_NAME'].' MOB.NO: '.$inv_mst_data_row['CUSTOMER_MOBILENO'].' BILL DT.: '.date("d-m-Y").' BILL NO.: '.$inv_mst_data_row['INV_NO'].' INVOICE TYPE OF WORK AMOUNT '; $total=0; $inv_det_query = "SELECT T2.PRODUCT_NAME, T2.AMOUNT FROM INVOICE_DET T2 WHERE T2.MST_ID='".$MST_ID."' "; $inv_det_results = mysqli_query($con,$inv_det_query); while($inv_det_data_row = mysqli_fetch_array($inv_det_results, MYSQLI_ASSOC)) < $content .= ' '.$inv_det_data_row['PRODUCT_NAME'].'
Write any remarks '.$inv_det_data_row['AMOUNT'].' '; $total=$total+$inv_det_data_row['AMOUNT']; > $content .= '------------------------ GRAND TOTAL: '.$total.' ------------------------ PAYMENT MODE: CASH/ONLINE THANK YOU ! VISIT AGAIN
'; $pdf->writeHTML($content); $file_location = "/home/fbi1glfa0j7p/public_html/examples/generate_pdf/uploads/"; //add your full path of your server //$file_location = "/opt/lampp/htdocs/examples/generate_pdf/uploads/"; //for local xampp server $datetime=date('dmY_hms'); $file_name = "INV_".$datetime.".pdf"; ob_end_clean(); if($_GET['ACTION']=='VIEW') < $pdf->Output($file_name, 'I'); // I means Inline view > else if($_GET['ACTION']=='DOWNLOAD') < $pdf->Output($file_name, 'D'); // D means download > else if($_GET['ACTION']=='UPLOAD') < $pdf->Output($file_location.$file_name, 'F'); // F means upload PDF file on some folder echo "Upload successfully!!"; > //----- End Code for generate pdf > else < echo 'Record not found for PDF.'; >?>
Once your created above file you must have to include TCPDF library include_once('tcpdf_6_2_13/tcpdf/tcpdf.php'); and that library you can download from below Source Code button. We provide full source code so can easily configure or setup your project for dynamic PDF generation.
Kindly note below some points or configuration while setup this code.
1) When you required different page size instead of A4 at that time you must have to add below code.
$pdf->AddPage('P','A5'); //when you require custom page size
2) When you want to view PDF file.
$pdf->Output($file_name, 'I'); // I means Inline view
3) When you need to Download PDF on your local computer.
$pdf->Output($file_name, 'D'); // D means download
4) Upload your dynamic PDF file on server.
$file_location = "/home/fbi1glfa0j7p/public_html/examples/generate_pdf/uploads/"; //add your full path of your server $pdf->Output($file_location.$file_name, 'F'); // F means upload PDF file on some folder