Send PDF Attachment

By Toledoh - October 24, 2016

Hi Guys.

I'm trying to use the following.  It allows a user to submit an email address which then sends an email to that address.  The core works fine, but how do I attached a PDF of the current page to that email using the createPDF plugin?

// process form
if (@$_REQUEST['submitForm']) {

// error checking
$errorsAndAlerts = "";
if (!@$_REQUEST['email']) { $errorsAndAlerts .= "You must enter an email to send to<br/>\n"; }

if (!$errorsAndAlerts) {

// Send Admin Alert via Email Template
$emailHeaders = emailTemplate_loadFromDB(array(
'template_id' => 'EMAIL-REPORT-FORM',
'placeholders' => array(
'user.email' => $_REQUEST['email'],
'user.comment' => $_REQUEST['message'],

'attachments' => array(
'sample.pdf' => createPDF_fromHTML('<h1>HTML As Inline</h1>')
),

)));
$mailErrors = sendMessage($emailHeaders);

//
$errorsAndAlerts = "Thanks! We've sent your email.";
$_REQUEST = array(); // clear form values
}
}

Cheers,

Tim (toledoh.com.au)

By Dave - October 25, 2016 - edited: October 25, 2016

Hey Tim, 

Like the same page you're on?  That's a bit tricky.  How we do it with CreatePDF is we setup some code to buffer the output and then run when the page is finished loading.  

What I'd recommend instead is putting the content of the page in an include, eg: mypage.php and mypage_content.php.  Then include "mypage_content.php" from the parent page.

Then, you can include it again when you want to use it's HTML to create a PDF, like this:

// get PDF of page content
$html    = ob_capture(function() { include("./mypage_content.php"); }); // capture output of included file
$pdfData = createPDF_fromHTML($html);
// createPDF_display('inline', $pdfData, 'example.pdf'); exit; // debug: test displaying PDF

Then you pass that to sendMessage like this:

'attachments' => array('sample.pdf' => $pdfData), 

Let me know if that works for you.

Dave Edis - Senior Developer
interactivetools.com

By Dave - October 25, 2016

Yes, thanks!  (I fixed the original post)

Dave Edis - Senior Developer
interactivetools.com