createPDF and send file as attachement

By ht1080z - September 28, 2015 - edited: September 28, 2015

Hi,

How can i store (not temporarily) in a selected folder and send immediately as attachment a PDF file after creation?

Any suggestions are welcome,
Karls

By Dave - September 30, 2015

Hi Karls, 

Here's some sample code from: Admin > Plugins > Create PDF > Example Code

// From /plugins/createPDF/examples/html_as_inline.php "HTML variable as inline PDF"
$html = '<h1>HTML As Inline</h1>';
$data = createPDF_fromHTML($html);
createPDF_display('inline', $data, 'example.pdf');

You can use this to get the PDF content in $data and then email that.  Here's an example:

// Send as email
$errors = sendMessage(array(
  'from'    => "dave@interactivetools.com",
  'to'      => "dave@interactivetools.com",
  'subject' => "Email with attached PDF",
  'text'    => "Here's the PDF attachment!\n",
  'attachments' => array(
    'sample.pdf'  => createPDF_fromHTML('<h1>HTML As Inline</h1>')
  ),
));
if ($errors) { die($errors); }

Let me know if that works for you!

Dave Edis - Senior Developer
interactivetools.com

By ht1080z - September 30, 2015

Hi Dave,

Can you send me a quick hint how can i set the destination/source folders on each script please?

Thank you in advance,
Karls

By ht1080z - September 30, 2015 - edited: September 30, 2015

Hi Dave,

1. Originally I wanted to store the created PDF file permanently in a selected folder on my server,
2. i need to use an email template on the email,
3. and the attachment has a custom filename.

I created this with your hints:

$GLOBALS['CREATEPDF_CUSTOM_SWITCHES'] = '--disable-smart-shrinking --margin-left 5 --margin-right 5'; 

$url  = "https://www.example.com/invoice-create.php?orderId=".$sampleVar['orderId'];
$fileName = $sampleVar['orderId']).".pdf";
$data = createPDF_fromUrl($url);
file_put_contents('../invoices/'.$fileName, $data);

$defaultLang = strtoupper($CURRENT_USER['uilang']);
$tempName     = 'MEMBER-SEND-INVOICE-'.$defaultLang;
$emailHeaders = emailTemplate_loadFromDB(array(
    'template_id'  => $tempName,
    'placeholders' => array(
        'user.email'      => $CURRENT_USER['email'],
        'user.name'       => $CURRENT_USER['nickname'],
    ),
    'attachments' => array(
        $fileName         => $data
    )
));
$mailErrors = sendMessage($emailHeaders);
if ($mailErrors) { alert("Mail Error: $mailErrors"); }

For some reason the CREATEPDF_CUSTOM_SWITCHES are ignored and i cannot change the margins. (i use the latest version pdfcreate)

Items in the Outgoing Mail folder not store attachments, right?

Please make a note on the above if you have any.

Thank you,
Karls

By Dave - October 2, 2015

Hi Karls, 

You can try adding the help switch to ensure the switches are getting to CreatePDF: 

$GLOBALS['CREATEPDF_CUSTOM_SWITCHES'] = '--disable-smart-shrinking --margin-left 5 --margin-right 5  --extended-help';  

That should cause all the help out to be displayed.  If it's not, the switches aren't reaching Wkhtmltopdf, if it is displayed then you'll have to experiment more.  I'd try --margin-left 50 or 500 and see if that's visible.

And, no, outgoing mail doesn't store attachments (to prevent using too much disk space).

Dave Edis - Senior Developer
interactivetools.com