Skip to content

Instantly share code, notes, and snippets.

@ajitbohra
Last active March 6, 2024 09:07
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save ajitbohra/3eb0296265931ddb4e84 to your computer and use it in GitHub Desktop.
Save ajitbohra/3eb0296265931ddb4e84 to your computer and use it in GitHub Desktop.
mpdf: HTML/CSS to PDF & send pdf via email
<?php
/*
mPDF: Generate PDF from HTML/CSS (Complete Code)
*/
require_once( 'mpdf/mpdf.php'); // Include mdpf
$stylesheet = file_get_contents('assets/css/pdf.css'); // Get css content
$html = '<div id="pdf-content">
Your PDF Content goes here (Text/HTML)
</div>';
// Setup PDF
$mpdf = new mPDF('utf-8', 'A4-L'); // New PDF object with encoding & page size
$mpdf->setAutoTopMargin = 'stretch'; // Set pdf top margin to stretch to avoid content overlapping
$mpdf->setAutoBottomMargin = 'stretch'; // Set pdf bottom margin to stretch to avoid content overlapping
// PDF header content
$mpdf->SetHTMLHeader('<div class="pdf-header">
<img class="left" src="assets/img/pdf_header.png"/>
</div>');
// PDF footer content
$mpdf->SetHTMLFooter('<div class="pdf-footer">
<a href="http://www.lubus.in">www.lubus.in</a>
</div>');
$mpdf->WriteHTML($stylesheet,1); // Writing style to pdf
$mpdf->WriteHTML($html); // Writing html to pdf
// FOR EMAIL
$content = $mpdf->Output('', 'S'); // Saving pdf to attach to email
$content = chunk_split(base64_encode($content));
// Email settings
$mailto = $email;
$from_name = 'LUBUS PDF Test';
$from_mail = 'email@domain.com';
$replyto = 'email@domain.com';
$uid = md5(uniqid(time()));
$subject = 'mdpf email with PDF';
$message = 'Download the attached pdf';
$filename = 'lubus_mpdf_demo.pdf';
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/pdf; name=\"".$filename."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
$is_sent = @mail($mailto, $subject, "", $header);
//$mpdf->Output(); // For sending Output to browser
$mpdf->Output('lubus_mdpf_demo.pdf','D'); // For Download
exit;
?>
// Coming Soon
@thaithean
Copy link

thaithean commented Jan 27, 2017

not working sent fail

@ajitbohra
Copy link
Author

@thatithean email?

@vtulse
Copy link

vtulse commented May 24, 2017

we use same code but not getting pdf file, getting below code in inbox

--d9977ef635e7ce5d0afb62c1bb2d569c

Content-Type: application/pdf; name="Daily - 22_05_17(24 hours).pdf"

Content-Transfer-Encoding: base64

Content-Disposition: attachment; filename="Daily - 22_05_17(24 hours).pdf"

JVBERi0xLjQKJeLjz9MKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMSAwIFIKL01lZGlh

Qm94IFswIDAgNTk1LjI4MCA4NDEuODkwXQovVHJpbUJveCBbMC4wMDAgMC4wMDAgNTk1LjI4MCA4

NDEuODkwXQovUmVzb3VyY2VzIDIgMCBSCi9Hcm91cCA8PCAvVHlwZSAvR3JvdXAgL1MgL1RyYW5z

cGFyZW5jeSAvQ1MgL0RldmljZVJHQiA+PiAKL0NvbnRlbnRzIDQgMCBSPj4KZW5kb2JqCjQgMCBv......

Please hep me

@YuvrajKhavad
Copy link

@ajitbohra There is no "mpdf.php" file in attachment. also there we try to download from https://github.com/mpdf/mpdf but here mpdf .php related to other folder.

@praveenkumarS7
Copy link

It works like a charm. You can download it from http://www.mpdfonline.com/repos/MPDF_5_7.zip. And extract it within ur project. Then You can use this code for better functionality.Please let me know if any issues arrive. (praveenkumar.s@hubino.com)

@jartavia05
Copy link

jartavia05 commented Aug 9, 2017

I tested today using mpdf 5 .7 and It works perfectly, thank you very much.

@luckylion999
Copy link

luckylion999 commented Sep 5, 2017

Hello,
I used above code. but email sending doesn't work.
I need to do this asap.
Can you please help me?

@waqarsabir
Copy link

I am using mpdf it is working perfect with me. But i want to send multiple attachments. I am able to send two emails with one attachments each. But i want to send both the attachments in one email. Can you please guide.

@priyankasailor
Copy link

mdpf not working somanyerrors are there plz help me

@NibyP
Copy link

NibyP commented Feb 19, 2018

it's not working for me. Getting "Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead". Please help me.

Copy link

ghost commented May 11, 2018

Hi,
How to setup send pdf or html Reports on Daily Basics from librenms server.Pls Help Me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment