If you are trying to create dynamic pdf file from mysql database and then after you want to send that dynamically generated pdf file as attachment with email, this is a very common feature in most of enterprise level web application. Because if any large web application then we have to send email with dynamically generated pdf file for providing such type of data information. So, in this post we have conver this topic in which we have learn how can we create dynamic pdf file from mysql database by using domPDF library and then after we will send that dynamically created pdf file will send as attachment with email using PHPMailer in PHP script. So, here we have learn PHP send email with PDF attachment.
In this tutorial, we will describe you how to fetch data from Mysql database and create PDF from that data and then after attach that PDF file to an HTML email and lastly send it. Here we have use domPDF for PDF generation and PHPMailer for sending email.
After searching on internet regarding how to create PDF file in PHP, so after searching we have found dompdf library which is the best library for create PDF file from HTML. This library simply convert HTML code of any web page and converts it to a PDF. And for sending email we have found PHPMailer library, this library has been easy to use and it can send HTML email with file attachment. So, by using both library we have make our task like create dynamic pdf file and send email as attachment.
Before learning this topic, in your mind will generate one question how to generate pdf file and then after it will be send as attachment, so here we will be creating PDF file temporary by using domPDF library which a library which has convert HTML info PDF file and then after by using PHPMailer which has been used for send email in our PHP script and after sending of email with attached PDF file, that file will be removed from folder. So, this is whole process of creating PDF file and sending email with attachment in PHP script using Dompdf and PHPMailer library.
In this tutorial, we will describe you how to fetch data from Mysql database and create PDF from that data and then after attach that PDF file to an HTML email and lastly send it. Here we have use domPDF for PDF generation and PHPMailer for sending email.
After searching on internet regarding how to create PDF file in PHP, so after searching we have found dompdf library which is the best library for create PDF file from HTML. This library simply convert HTML code of any web page and converts it to a PDF. And for sending email we have found PHPMailer library, this library has been easy to use and it can send HTML email with file attachment. So, by using both library we have make our task like create dynamic pdf file and send email as attachment.
Before learning this topic, in your mind will generate one question how to generate pdf file and then after it will be send as attachment, so here we will be creating PDF file temporary by using domPDF library which a library which has convert HTML info PDF file and then after by using PHPMailer which has been used for send email in our PHP script and after sending of email with attached PDF file, that file will be removed from folder. So, this is whole process of creating PDF file and sending email with attachment in PHP script using Dompdf and PHPMailer library.
Source Code
index.php
<?php
//index.php
$message = '';
$connect = new PDO("mysql:host=localhost;dbname=testing", "root", "");
function fetch_customer_data($connect)
{
$query = "SELECT * FROM tbl_customer";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$output = '
<div class="table-responsive">
<table class="table table-striped table-bordered">
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th>Postal Code</th>
<th>Country</th>
</tr>
';
foreach($result as $row)
{
$output .= '
<tr>
<td>'.$row["CustomerName"].'</td>
<td>'.$row["Address"].'</td>
<td>'.$row["City"].'</td>
<td>'.$row["PostalCode"].'</td>
<td>'.$row["Country"].'</td>
</tr>
';
}
$output .= '
</table>
</div>
';
return $output;
}
if(isset($_POST["action"]))
{
include('pdf.php');
$file_name = md5(rand()) . '.pdf';
$html_code = '<link rel="stylesheet" href="bootstrap.min.css">';
$html_code .= fetch_customer_data($connect);
$pdf = new Pdf();
$pdf->load_html($html_code);
$pdf->render();
$file = $pdf->output();
file_put_contents($file_name, $file);
require 'class/class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP(); //Sets Mailer to send message using SMTP
$mail->Host = 'smtpout.secureserver.net'; //Sets the SMTP hosts of your Email hosting, this for Godaddy
$mail->Port = '80'; //Sets the default SMTP server port
$mail->SMTPAuth = true; //Sets SMTP authentication. Utilizes the Username and Password variables
$mail->Username = 'xxxxxxxxxx'; //Sets SMTP username
$mail->Password = 'xxxxxxxxxx'; //Sets SMTP password
$mail->SMTPSecure = ''; //Sets connection prefix. Options are "", "ssl" or "tls"
$mail->From = 'info@webslesson.info'; //Sets the From email address for the message
$mail->FromName = 'Webslesson.info'; //Sets the From name of the message
$mail->AddAddress('web-tutorial@programmer.net', 'Name'); //Adds a "To" address
$mail->WordWrap = 50; //Sets word wrapping on the body of the message to a given number of characters
$mail->IsHTML(true); //Sets message type to HTML
$mail->AddAttachment($file_name); //Adds an attachment from a path on the filesystem
$mail->Subject = 'Customer Details'; //Sets the Subject of the message
$mail->Body = 'Please Find Customer details in attach PDF File.'; //An HTML or plain text message body
if($mail->Send()) //Send an Email. Return true on success or false on error
{
$message = '<label class="text-success">Customer Details has been send successfully...</label>';
}
unlink($file_name);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Create Dynamic PDF Send As Attachment with Email in PHP</title>
<script src="jquery.min.js"></script>
<link rel="stylesheet" href="bootstrap.min.css" />
<script src="bootstrap.min.js"></script>
</head>
<body>
<br />
<div class="container">
<h3 align="center">Create Dynamic PDF Send As Attachment with Email in PHP</h3>
<br />
<form method="post">
<input type="submit" name="action" class="btn btn-danger" value="PDF Send" /><?php echo $message; ?>
</form>
<br />
<?php
echo fetch_customer_data($connect);
?>
</div>
<br />
<br />
</body>
</html>
pdf.php
<?php
//pdf.php
require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
class Pdf extends Dompdf{
public function __construct(){
parent::__construct();
}
}
?>
How do i do this in laravel
ReplyDeleteHi the file is corrupted
ReplyDeleteare share the class/class.phpmailer.php lib link..
ReplyDeleteThis does not work at all.!
ReplyDeleteFor those who said this didn't work, please execute this SQL command on the database to create 'tbl_customer' table first!
ReplyDeleteCREATE TABLE `tbl_customer` (
`CustomerName` varchar(256) COLLATE utf8mb4_unicode_ci NOT NULL,
`Address` varchar(256) COLLATE utf8mb4_unicode_ci NOT NULL,
`City` varchar(256) COLLATE utf8mb4_unicode_ci NOT NULL,
`PostalCode` varchar(256) COLLATE utf8mb4_unicode_ci NOT NULL,
`Country` varchar(256) COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
COMMIT;
Then, in the index.php, change dbname into your database name:
$connect = new PDO("mysql:host=localhost;dbname=YourDB", "root", "");
where is "class.phpmailer.php"?
ReplyDeletenot working
ReplyDeletewhere is database?
ReplyDeletehow to add pagebreak please help me.
ReplyDeletesir please Create Dynamic PDF & Send as Attachment with bulk Email in PHP
ReplyDeleteThere no database includes in source code
ReplyDeleteWorking , tanks
ReplyDeleteHi, Thanks for help, I'm getting these errors. Can you help?
ReplyDeleteWarning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in C:\xampp\htdocs\pdf-mail\dompdf\src\Dompdf.php on line 600
Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in C:\xampp\htdocs\pdf-mail\dompdf\src\Dompdf.php on line 621
Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in C:\xampp\htdocs\pdf-mail\dompdf\lib\Cpdf.php on line 469
Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in C:\xampp\htdocs\pdf-mail\dompdf\lib\Cpdf.php on line 476
Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in C:\xampp\htdocs\pdf-mail\dompdf\lib\Cpdf.php on line 483
Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in C:\xampp\htdocs\pdf-mail\dompdf\lib\Cpdf.php on line 490
Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in C:\xampp\htdocs\pdf-mail\dompdf\src\Css\Stylesheet.php on line 1340
use this version of Dompdf
Deletehttps://github.com/dompdf/dompdf/releases/tag/v0.8.3
This error is not gone :
DeleteFatal error: Uncaught TypeError: Unsupported operand types: string + float in C:\xampp\htdocs\pdfmail\dompdf\src\Renderer\Inline.php:153 Stack trace: #0 C:\xampp\htdocs\pdfmail\dompdf\src\Renderer.php(293): Dompdf\Renderer\Inline->render(Object(Dompdf\FrameDecorator\Inline)) #1 C:\xampp\htdocs\pdfmail\dompdf\src\Renderer.php(108): Dompdf\Renderer->_render_frame('inline', Object(Dompdf\FrameDecorator\Inline)) #2 C:\xampp\htdocs\pdfmail\dompdf\src\Renderer.php(194): Dompdf\Renderer->render(Object(Dompdf\FrameDecorator\Inline)) #3 C:\xampp\htdocs\pdfmail\dompdf\src\Renderer.php(194): Dompdf\Renderer->render(Object(Dompdf\FrameDecorator\TableCell)) #4 C:\xampp\htdocs\pdfmail\dompdf\src\Renderer.php(194): Dompdf\Renderer->render(Object(Dompdf\FrameDecorator\TableRow)) #5 C:\xampp\htdocs\pdfmail\dompdf\src\Renderer.php(194): Dompdf\Renderer->render(Object(Dompdf\FrameDecorator\TableRowGroup)) #6 C:\xampp\htdocs\pdfmail\dompdf\src\Renderer.php(194): Dompdf\Renderer->render(Object(Dompdf\FrameDecorator\Table)) #7 C:\xampp\htdocs\pdfmail\dompdf\src\Renderer.php(194): Dompdf\Renderer->render(Object(Dompdf\FrameDecorator\Block)) #8 C:\xampp\htdocs\pdfmail\dompdf\src\FrameReflower\Page.php(148): Dompdf\Renderer->render(Object(Dompdf\FrameDecorator\Block)) #9 C:\xampp\htdocs\pdfmail\dompdf\src\FrameDecorator\AbstractFrameDecorator.php(895): Dompdf\FrameReflower\Page->reflow(NULL) #10 C:\xampp\htdocs\pdfmail\dompdf\src\Dompdf.php(847): Dompdf\FrameDecorator\AbstractFrameDecorator->reflow() #11 C:\xampp\htdocs\pdfmail\index.php(53): Dompdf\Dompdf->render() #12 {main} thrown in C:\xampp\htdocs\pdfmail\dompdf\src\Renderer\Inline.php on line 153
its not working
ReplyDeleteThanks its working perfectly.....
ReplyDeletehow to create smtp username and password
ReplyDeletehow to create this ingooics hosting panel
Deletehow to create smtp username and password
ReplyDeletethis script is not working
ReplyDeleteHi, thank youm the code works well and was adaptable to suit my need.
ReplyDeleteOne question though, how could a logo be added that will render in the pdf?
TYI :)
how to add image inside pdf?
ReplyDeletewhere is the sql file
ReplyDeletecan you show this program using oci database .
ReplyDeleteit sending blank pdf page, no my table. I am using local server, sending email to gmail successfully, but the attached doc is blank. what did I do wrong? please help. THANKS.
ReplyDeletehow to send attachment using bootstrap modal
ReplyDeleteHi,
ReplyDeleteTried the code its generating the PDF but not sending the email. Checked email setting can you please add demo version with the details where we have to change setting will be very much helpful.
Hello, can anyone tell me how should I make unicode (UTF-8) work?
ReplyDeletehow to change pdf size
ReplyDeleteneed to look like landscape
Thank You Very Helpful
ReplyDeleteVery Well
ReplyDeleteHow to send pdf emails with html
ReplyDelete$query = "SELECT * FROM request WHERE id=$id";
ReplyDeletehow do i make the where clause to work