mPDF Manual – Real life examples

E-mail a PDF file

This example shows how to create a PDF file and e-mail it:

// require composer autoload
require __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf(); // Create new mPDF Document

// Beginning Buffer to save PHP variables and HTML tags

$day = date('d');
$year = date('Y');
$month = date('F');

echo "Hello World

Today is $month $day, $year";

$html = ob_get_contents();

// Here convert the encode for UTF-8, if you prefer 
// the ISO-8859-1 just change for $mpdf->WriteHTML($html);
$content = $mpdf->Output('', 'S');

E-mailing with attachments by yourself is full of unexpected obstacles. Instead of building the message by hand, we will use a 3rd party library.

E-mailing with Swift mailer

First, require Swift mailer with command

$ composer require swiftmailer/swiftmailer

Then, create a Swift Message and send it with a Transport wrapping PHP mail() function.

// Create instance of Swift_Attachment with our PDF file
$attachment = new Swift_Attachment($content, 'filename.pdf', 'application/pdf');

$message = Swift_Message::newInstance()
  ->setSubject('Your subject')
  ->setFrom(array('' => 'John Doe'))
  ->setTo(array('', '' => 'A name'))
  ->setBody('Here is the message itself')

$transport = Swift_MailTransport::newInstance();

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Send the created message

// Then, you can send PDF to the browser
$mpdf->Output($filename ,'I');

For more information about Swift Mailer, see its documentation.

Fork me on GitHub