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

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 Dave - September 30, 2015

Hi Karls, 

That code example doesn't require any files.  The file data is just stored in memory until it's sent and the email recipient can choose where to save the attachment.

But you can create files like this:

file_put_contents('hello.txt', 'Hello World'); 

And read them in like this:

$data = file_get_contents('hello.txt'); 

What are you trying to do specifically?

Dave Edis - Senior Developer
interactivetools.com

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