I wanted to be able to use mimemail to send a print_pdf file as an attachment. Currently the print_pdf module streams the PDF as a file; in order to add the PDF as an attachment it needs to be returned as a string to be used by (for example) the mimemail module.

This also requires mimemail to be patched with #932962: Allow attachments to be added by contents

With the attached patch, the following code can be modified to create a PDF email attachment from another module:

  module_load_include('inc', 'print_pdf', 'print_pdf.pages');  // require print_pdf.pages.inc

  // set up email
  $recipients[] = email@example.com;  
  $subject = "Your PDF is attached";
  $body = '<p>Your PDF is attached</p>';
  $path = 'node/' . $node->nid;
  $attachments[] = array(
    'contents' => print_pdf_controller($path),  // call the print_pdf function to generate PDF contents string
    'filemime' => 'application/pdf',
    'filename' => 'filename.pdf',
  );

  // send email
  mimemail( $settings['from'], $recipients, $subject, $body, FALSE, array(), drupal_html_to_text(decode_entities($body)), $attachments );

Comments

ob3ron’s picture

StatusFileSize
new4.1 KB

I forgot to add, the above patch was written for use with dompdf (which I found to have the best CSS support, and is being actively developed, contrary to the print module description).

Here is another patch with TCPDF and wkhtmltopdf also invited to the party. I have not tested support for these last two converters so it would be great to hear if this works from someone who uses them.

jcnventura’s picture

Status: Needs review » Fixed

Hi,

Thanks for the patch.. I've changed the module to support two calls:

print_pdf_generate_path($path, $cid = NULL, $pdf_filename = NULL)

and

print_pdf_generate_html($print, $html, $filename = NULL)

which allow you to get a PDF based on $path (applies the print.tpl.php and the rest) or on the $html (whatever HTML you pass it).

ob3ron’s picture

Hi João,

What is the procedure to call the print_pdf_generate_path function from another module? Your change breaks my implementation above.

Thanks for incorporating this functionality!

jcnventura’s picture

arvana, yes, sorry for breaking your implementation.. The extra argument seemed useless, while used together with filename, and my previous plans for a PDF API was to allow other modules to use it for their own HTML, so I really wanted to provide those two functions.

I think that to use the above calls you have to use module_include to include the print_pdf.pages.inc file..

ob3ron’s picture

Status: Fixed » Active

Hi João,

I did include the print_pdf.pages.inc file... As far as I can see, the two calls you've added do not give the functionality that my implementation needs, namely to create a PDF string from a path, which can then be added as a file attachment via mimemail.

Am I wrong? If so how would I modify the module code shown above to call print_pdf and send a string to mimemail?

ob3ron’s picture

Component: Code » Documentation

OK, I finally figured out how you worked it. Sorry I missed it before. :)

For sending a PDF via mimemail, this is how to call the print_pdf function:

<?php
  module_load_include('inc', 'print_pdf', 'print_pdf.pages');  // require print_pdf.pages.inc

  // set up email
  $recipients[] = email@example.com; 
  $subject = "Your PDF is attached";
  $body = '<p>Your PDF is attached</p>';
  $path = 'node/' . $node->nid;
  $attachments[] = array(   // attach ticket
    'filecontent' => print_pdf_generate_path($path),  // call the print_pdf function to generate PDF content string
    'filemime' => 'application/pdf',
    'filename' => $filename,
  );

  // send email
  mimemail( $settings['from'], $recipients, $subject, $body, FALSE, array(), drupal_html_to_text(decode_entities($body)), $attachments );
?>

Can I suggest that this be added in to the module documentation?

jcnventura’s picture

That would fit really well into http://drupal.org/node/190173 !!

Can you add that yourself, please? :)

jcnventura’s picture

Status: Active » Closed (fixed)

Added it to the FAQ.

asghar’s picture

HI...

could you explain me what do you mean by

'filecontent' => print_pdf_generate_path($path), // call the print_pdf function to generate PDF content string

I cannot find any function with nname "print_pdf_generate_path($path)" in module as well as patches. ?

jcnventura’s picture

It's in the print_pdf/print_pdf.pages.inc file.. You need to include it explicitly before using it.

pieterdc’s picture

@asghar: If it's not in the file @jcnventura tells you. Then, you're probably not yet using the latest dev version of the print module.

ryanfc78’s picture

What file do you add this code to? Can anyone tell me where to implement this code or send me a link that gives directions? I have the Mimemail module installed as well as the Printer, e-mail and PDF versions module installed. Thanks for any help.

liminu’s picture

How can i change the sender email?
Solve, use the dev module

cjsmith87’s picture

Issue summary: View changes
function mycustommodule_menu() {
    $items['send/invoice'] = array(
      'page callback' => 'mycustommodule_email_invoice',
	  'page arguments' => array(2),
	  'access arguments' => array('access content'), 
	  'type' => MENU_CALLBACK,
    );
    return $items;
  }

 function mycustommodule_email_invoice($uid = 0, $nodeid = 0) {
  
	module_load_include('inc', 'print_pdf', 'print_pdf.pages');
	
	$myuser = user_load($uid);
	$main_contact = field_get_items('user', $myuser, 'field_main_contact');
   
	$subject = 'Invoice Attached';
        $body = 'Dear '. $main_contact[0]['value'] . ',<br><br>';
	$body = 'Please find your latest invoice attached';
  
	// set up email
	$recipients[] = $myuser->name . ' <'. $myuser->mail . '>';
	$path = 'node/' . $nodeid;
	$attachments[] = array( 
	'filecontent' => print_pdf_generate_path($path),
	'filemime' => 'application/pdf',
	'filename' => 'Your Invoice',
	);
  
	mimemail( 'sales@mywebsite.com', $recipients, $subject, $body, FALSE, array(), drupal_html_to_text(decode_entities($body)), $attachments );
	drupal_set_message(t('Reminder email sent for ') . $myuser->name , 'status');
  
	$queries = drupal_get_query_parameters();
	if(isset($queries['destination']))
		{
		drupal_goto($queries['destination']);
		}
		drupal_goto('<front>');

}

The above code does not work and im not sure why? It redirects to a 404 page not found and no mail is sent? I know the link is working because if I take out the attachment and email sending it works fine. What am I missing?

jimmyyy’s picture

print_pdf function can be called to create PDF doc file. More other functions?