MPDF E-mail Attachment Sends Blank PDF
Asked Answered
S

2

2

I have successfully generated a PDF using mpdf, which I have verified by downloading the PDF. However, when I send the PDF as an e-mail attachment I receive a blank PDF with an "Out of Memory" error by Adobe Reader. Below is my code:

<?php

include("MPDF57/mpdf.php");

ob_start();
include "Receipt_Template_2.php"; 
$template = ob_get_contents();
ob_end_clean();

$mpdf=new mPDF('','A4','','',32,25,27,25,16,13,'L'); 

mpdf->WriteHTML($template);

$content = $mpdf->Output($template, 'S');

$content = chunk_split(base64_encode($content));
$mailto = '[email protected]';
$from_name = 'KIREA';
$from_mail = '[email protected]';
$uid = md5(uniqid(time()));
$subject = 'KIREA Donation Receipt';
$message = "Thank you for your donation!\n\nAttached is the receipt concerning the     donation. If you have any questions, please e-mail us at [email protected]";;
$filename = $pdfName;

$header = "From: ".$from_name." <".$from_mail.">\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\n";
$header .= "This is a multi-part message in MIME format.\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\n";
$header .= "Content-Transfer-Encoding: 7bit\n";
$header .= $message."\n\r\n";
$header .= "--".$uid."\n";
$header .= "Content-Type: application/pdf; name=\"".$filename."\"\n";
$header .= "Content-Transfer-Encoding: base64\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\n\n";
$header .= $content."\n\n";
$header .= "--".$uid."--";

$is_sent = @mail($mailto, $subject, "", $header);

$mpdf->Output();
exit;

?>

Are there any ideas as to why the PDF is ending up blank after being sent as an e-mail attachment? Thank you.

Stolen answered 31/10, 2013 at 22:49 Comment(3)
WEre you able to solve this? Having the same prob.Babbette
This might help: mpdf1.com/manual/index.php?tid=373Nahshunn
Manual is offline. Use this instead: mpdf.github.io/real-life-examples/e-mail-a-pdf-file.htmlMast
C
1
mpdf->WriteHTML($template);
$content = $mpdf->Output($template, 'S');

You are wrong at here you are not taking the object on which your data is written

please replace below code with above to get correct result.

$pdfdata=mpdf->WriteHTML($template);
$content = $mpdf->Output('' , 'S');

use $content in your email

Callender answered 23/5, 2017 at 13:3 Comment(0)
N
0

If you can use swiftmailer, you can attach a MPDF generated PDF to the email, quite easily as follows:

<?php
require_once $swift_mailer_path.'swift_required.php';

$transporter = Swift_SmtpTransport::newInstance($smtp_host, $smtp_port, $smtp_protocol)
  ->setUsername($smtp_username')
  ->setPassword($smtp_password');

$mailer = Swift_Mailer::newInstance($transporter);

$message = Swift_Message::newInstance('Email Subject')
  ->setFrom(array($from_email => $from_name))
  ->setTo($to_email)
  ->setBody($email_body);

$attachment = Swift_Attachment::newInstance($mpdf->Output($pdf_path, "S"), $pdf_file_name, 'application/pdf');
$message->attach($attachment);  

$message->setContentType("text/html");

$result = $mailer->send($message);
?>

Here is the Reference.

Nahshunn answered 14/1, 2016 at 9:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.