web 2.0

How to usd PHP & PDF - Export MySQL to PDF and Send Email Attachment

How to usd PHP & PDF - Export MySQL to PDF and Send Email Attachment Learn PHP how to using PHP export to pdf file and send mail attachment file.

ShotDev Focus:
- PHP & PDF (Export MySQL to PDF and send mail attachment)

Example

php_pdf_send_mail.php

  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <?php  
  7. require('fpdf.php');  
  8.   
  9. class PDF extends FPDF  
  10. {  
  11. //Load data  
  12. function LoadData($file)  
  13. {  
  14. //Read file lines  
  15. $lines=file($file);  
  16. $data=array();  
  17. foreach($lines as $line)  
  18. $data[]=explode(';',chop($line));  
  19. return $data;  
  20. }  
  21.   
  22. //Simple table  
  23. function BasicTable($header,$data)  
  24. {  
  25. //Header  
  26. $w=array(30,30,55,25,20,20);  
  27. //Header  
  28. for($i=0;$i<count($header);$i++)  
  29. $this->Cell($w[$i],7,$header[$i],1,0,'C');  
  30. $this->Ln();  
  31. //Data  
  32. foreach ($data as $eachResult)  
  33. {  
  34. $this->Cell(30,6,$eachResult["CustomerID"],1);  
  35. $this->Cell(30,6,$eachResult["Name"],1);  
  36. $this->Cell(55,6,$eachResult["Email"],1);  
  37. $this->Cell(25,6,$eachResult["CountryCode"],1,0,'C');  
  38. $this->Cell(20,6,$eachResult["Budget"],1);  
  39. $this->Cell(20,6,$eachResult["Budget"],1);  
  40. $this->Ln();  
  41. }  
  42. }  
  43.   
  44. //Better table  
  45. function ImprovedTable($header,$data)  
  46. {  
  47. //Column widths  
  48. $w=array(20,30,55,25,25,25);  
  49. //Header  
  50. for($i=0;$i<count($header);$i++)  
  51. $this->Cell($w[$i],7,$header[$i],1,0,'C');  
  52. $this->Ln();  
  53. //Data  
  54.   
  55. foreach ($data as $eachResult)  
  56. {  
  57. $this->Cell(20,6,$eachResult["CustomerID"],1);  
  58. $this->Cell(30,6,$eachResult["Name"],1);  
  59. $this->Cell(55,6,$eachResult["Email"],1);  
  60. $this->Cell(25,6,$eachResult["CountryCode"],1,0,'C');  
  61. $this->Cell(25,6,number_format($eachResult["Budget"],2),1,0,'R');  
  62. $this->Cell(25,6,number_format($eachResult["Budget"],2),1,0,'R');  
  63. $this->Ln();  
  64. }  
  65. //Closure line  
  66. $this->Cell(array_sum($w),0,'','T');  
  67. }  
  68.   
  69. //Colored table  
  70. function FancyTable($header,$data)  
  71. {  
  72. //Colors, line width and bold font  
  73. $this->SetFillColor(255,0,0);  
  74. $this->SetTextColor(255);  
  75. $this->SetDrawColor(128,0,0);  
  76. $this->SetLineWidth(.3);  
  77. $this->SetFont('','B');  
  78. //Header  
  79. $w=array(20,30,55,25,25,25);  
  80. for($i=0;$i<count($header);$i++)  
  81. $this->Cell($w[$i],7,$header[$i],1,0,'C',true);  
  82. $this->Ln();  
  83. //Color and font restoration  
  84. $this->SetFillColor(224,235,255);  
  85. $this->SetTextColor(0);  
  86. $this->SetFont('');  
  87. //Data  
  88. $fill=false;  
  89. foreach($data as $row)  
  90. {  
  91. $this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);  
  92. $this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);  
  93. $this->Cell($w[2],6,$row[2],'LR',0,'L',$fill);  
  94. $this->Cell($w[3],6,$row[3],'LR',0,'C',$fill);  
  95. $this->Cell($w[4],6,number_format($row[4]),'LR',0,'R',$fill);  
  96. $this->Cell($w[5],6,number_format($row[5]),'LR',0,'R',$fill);  
  97. $this->Ln();  
  98. $fill=!$fill;  
  99. }  
  100. $this->Cell(array_sum($w),0,'','T');  
  101. }  
  102. }  
  103.   
  104. $pdf=new PDF();  
  105. //Column titles  
  106. $header=array('CustomerID','Name','Email','Country Code','Budget','Used');  
  107. //Data loading  
  108.   
  109. //*** Load MySQL Data ***//  
  110. $objConnect = mysql_connect("localhost","root","root"or die(mysql_error());  
  111. $objDB = mysql_select_db("mydatabase");  
  112. $strSQL = "SELECT * FROM customer";  
  113. $objQuery = mysql_query($strSQL);  
  114. $resultData = array();  
  115. for ($i=0;$i<mysql_num_rows($objQuery);$i++) {  
  116. $result = mysql_fetch_array($objQuery);  
  117. array_push($resultData,$result);  
  118. }  
  119. //************************//  
  120.   
  121. $pdf->SetFont('Arial','',10);  
  122.   
  123. //*** Table 1 ***//  
  124. $pdf->AddPage();  
  125. $pdf->Image('logo.png',80,8,33);  
  126. $pdf->Ln(35);  
  127. $pdf->BasicTable($header,$resultData);  
  128.   
  129. //*** Table 2 ***//  
  130. $pdf->AddPage();  
  131. $pdf->Image('logo.png',80,8,33);  
  132. $pdf->Ln(35);  
  133. $pdf->ImprovedTable($header,$resultData);  
  134.   
  135. //*** Table 3 ***//  
  136. $pdf->AddPage();  
  137. $pdf->Image('logo.png',80,8,33);  
  138. $pdf->Ln(35);  
  139. $pdf->FancyTable($header,$resultData);  
  140.   
  141. $pdf->Output("shotdev/shotdev.pdf","F");  
  142.   
  143. //*************** Send Email ***************//  
  144.   
  145. $strTo = "member@shotdev.com";  
  146. $strSubject = "PDF Report";  
  147. $strMessage = "Download mypdf.pdf for PDF Report";  
  148.   
  149. //*** Uniqid Session ***//  
  150. $strSid = md5(uniqid(time()));  
  151.   
  152. $strHeader = "";  
  153. $strHeader .= "From: Mr.Weerachai Nukitram<webmaster@shotdev.com>\nReply-To: webmaster@shotdev.com\n";  
  154. $strHeader .= "Cc: Mr.Surachai Sirisart<surachai@shotdev.com>";  
  155. $strHeader .= "Bcc: webmaster@shotdev.com";  
  156.   
  157. $strHeader .= "MIME-Version: 1.0\n";  
  158. $strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";  
  159. $strHeader .= "This is a multi-part message in MIME format.\n";  
  160.   
  161. $strHeader .= "--".$strSid."\n";  
  162. $strHeader .= "Content-type: text/html; charset=windows-874\n"// or UTF-8 //  
  163. $strHeader .= "Content-Transfer-Encoding: 7bit\n\n";  
  164. $strHeader .= $strMessage."\n\n";  
  165.   
  166. $strContent1 = chunk_split(base64_encode(file_get_contents("shotdev/mypdf.pdf")));  
  167. $strHeader .= "--".$strSid."\n";  
  168. $strHeader .= "Content-Type: application/octet-stream; name=\"mypdf.pdf\"\n";  
  169. $strHeader .= "Content-Transfer-Encoding: base64\n";  
  170. $strHeader .= "Content-Disposition: attachment; filename=\"mypdf.pdf\"\n\n";  
  171. $strHeader .= $strContent1."\n\n";  
  172.   
  173. $flgSend = @mail($strTo,$strSubject,null,$strHeader); // @ = No Show Error //  
  174. if($flgSend)  
  175. {  
  176. echo "PDF Generated & mail sending.";  
  177. }  
  178. else  
  179. {  
  180. echo "Cannot send mail.";  
  181. }  
  182. ?>  
  183. </body>  
  184. </html>  

Create a php file and save to path root-path/myphp/

Run
http://localhost/myphp/php_pdf_send_mail.php

.
.
.

Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (1 votes, average: 1.00 out of 10)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.