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?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

2pha’s picture

I think it is because of how many arguments you are sending the menu callback.
I don't think you are sending $nodeid, therefore the $path is not correct and 'filecontent' => print_pdf_generate_path($path) will fail.

ericwongcm’s picture

Version: 7.x-1.x-dev » 7.x-2.0

Any update on this?

I am looking for a way to use the "Send by email" link to sent a pdf version of the node instead of the html page.

2pha’s picture

@hdmp4.com,
The code above seems to be using the mimemail module (and an old version). I could not get it to work.
Below is a full module that I made for my site. I contains a fair amount of kruft that you will not need but it should get you going.


function custom_send_email_menu() {
  $items['sendemail/%'] = array(
    'title' => 'Send PDF',
    'page callback' => 'custom_send_email_page',
    'page arguments' => array(1),
    'access arguments' => array('send pdf email'),
  );
  return $items;
}


function custom_send_email_permission() {
  return array(
    'send pdf email' => array(
      'title' => t('Send PDF email'),
      'description' => t('Send a PDF email of job, report or worksheet.'),
    ),
  );
}

function custom_send_email_page($nid) {
  
  $node = node_load($nid);
  
  $customer = '';
  
  if(isset($node->type)){
    switch ($node->type) {
      case 'job':
        $customer = user_load($node->field_job_customer[LANGUAGE_NONE][0]['target_id']);
        //$email = $customer->mail;
        break;
      case 'service_report':
        $job = node_load($node->field_job[LANGUAGE_NONE][0]['target_id']);
        $customer = user_load($job->field_job_customer[LANGUAGE_NONE][0]['target_id']);
        break;
      case 'worksheet':
        $job = node_load($node->field_job[LANGUAGE_NONE][0]['target_id']);
        $customer = user_load($job->field_job_customer[LANGUAGE_NONE][0]['target_id']);
        break;
      
    }
    
    if(!empty($customer)){
      $sent_message = drupal_mail('custom_send_email', $node->title, $customer->mail, language_default(), array('node' => $node, 'user' => $customer));
      if($sent_message['result']){
        drupal_set_message($node->title.' email sent to '.$customer->mail);
      }else{
        drupal_set_message('Email not sent: An error occured.', 'error');
      }
    }else{
      drupal_set_message('Email not set: could not get customer', 'error');
    }
  }else{
    drupal_set_message('Email not sent: could not load node.', 'error');
  }
  
  if(isset($queries['destination']))
  {
    drupal_goto($queries['destination']);
  }
  drupal_goto('<front>');
  
  
  return 'email sent';
}

function custom_send_email_mail($key, &$message, $params) {

  $node = $params['node'];
  $user = $params['user'];
  
  $message['subject'] = t($node->title.' attached');
  $message['body'] = 'Hello '.$user->name.'<br><br>Please find '.$node->title.' attached.';
  //$message['params']['attachments'][] = $attachment;
  
  module_load_include('inc', 'print_pdf', 'print_pdf.pages');
  
  $attachment = array(
    'filecontent' => print_pdf_generate_path($node->nid),
    'filemime' => 'application/pdf',
    'filename' => $node->title.'.pdf',
  );
  $message['params']['attachments'][] = $attachment;
  
}
ilyasmdgh’s picture

@2pha I created a module from the above code and I could send the Email. But the PDF does not attach.
I am able to download the PDF version.

I did try kpr($message); and found that the attachment does have the variable 'filecontent' set as string and 98712 characters. The amount of characters make me to believe that the PDF is indeed created. Somehow the PDF is not attached.

I am at a dead end to debug the problem.

Can you please help me find the reason and solution for this.

Is there some sort of attachment problem with the server settings?

ilyasmdgh’s picture

I installed the mimemail and mailsystem modules and now the attachments are working fine. Did not think these modules are required. Thanks again for the module code.

misterpo’s picture

Hi,

Thanks for this code, works great.

Nevertheless, it seems that CSS are not applied to the generated PDF file.

I use dompdf and CSS to make my PDF pages look like what I want.

Any idea ?

misterpo’s picture

Hi,

Sorry to insist but I would like to understand how to get the same PDF file when sending it by e-mail as the one I get through the standard Print PDF function.

You will find both files attached.

apmsooner’s picture

@misterpo

I use the following modules:
1. Rules Link - for creating a link that performs rule via rules module
2. Rules Download - https://www.drupal.org/project/rules_download. Adds an action of "Download a file from the internet" to rules. Set the path to printpdf/[node:nid]. You'll have variables available to attach it to a node file field and/or then email it in step 3.
3. Mime Mail - send html email with attachment (from step 2)
- example for path to file here depending on how you're storing them: sites/default/files/private/documents/[node:field-document:file:name]

Works well without any additional custom coding. Additionally, If you want a form to pass the email values to your rule, try this module: https://www.drupal.org/project/rules_panes

Have fun.

EDIT - sorry didn't realize you actually were able to get the code to work but just having document rendering issues... not sure on that one.

misterpo’s picture

@apmsooner

Thanks a lot for the information, I will give a try to your solution.

I am currently stuck with this PDF rendering issue and cannot go further with the piece of code.

apmsooner’s picture

@misterpro,

There are some css limitations with some of those PDF renderers. Not sure which one your using but The best css support i've found was to use the PrinceXML engine to generate my pdfs. I use this via docraptor which is a 3rd party cloud service that handles the conversions. I made a print module plugin for this service if you want to give it a try... https://www.drupal.org/sandbox/apmsooner/2271521

Otherwise, i've heard wkhtmltopdf plugin has the best css support. I just needed a cloud based service to convert my pdfs for performance reasons and flexibility and this has worked out well for me.

misterpo’s picture

Hi apmsooner,

I dont't want to switch from dompdf to any other PDF generating solution as I have already made a lot of work to render them properly with dompdf.

I have given a try to your solution but I really can't figure out how to make Rules download and Mime Mail work together.

I send e-mails from a button located in a view so there is no trigger needed by rules...

misterpo’s picture

Hi,

I have finally figured out that my CSS problem was bound to URL restrictions I have applied to my CSS rule.

When I remove these restrictions, CSS are applied and the PDF file is (almost) like expected.

I'd like to remove the header ("Published on...") generated by the module (please see file attached).

How can I do that ?

misterpo’s picture

Adapting print.tpl.php fixed the problem.

thoriax’s picture

at apmsooner #8

I was trying to implement a similar functionality. Despite I got stuck..
By chance, I found your comment which indicates it should work..

I want to use the rules "Download a file from the internet" to download a file (which is generated by printpdf/[node:nid] )
My downloaded file seems to be the html page file, instead the generated pdf.
If i manually enter the page printpdf/[node:nid], I am able to download the pdf properly.
How did you manage to download the pdf instead the html full page using rules?

Thanks!

apmsooner’s picture

@thoriax, i'm doing it the same way as you. Maybe check your print settings in the pdf section and set the open pdf in save dialog instead of new window.

thoriax’s picture

Thanks for your feedback!
Unfortunately I already configured this one. Any other options in your mind?
Do you use dompdf to generate the pdf?

apmsooner’s picture

@thoriax, no i use a 3rd party to generate my pdf called docraptor. I helped create and maintain a drupal module that connects to it here: https://www.drupal.org/project/print_pdf_docraptor. I had issues with dompdf originally when trying to process large pdfs and the css support is terrible. The docraptor plugin satisfied my needs. Still... i would think the rules download module should be able to create the file regardless but perhaps you need to make sure the file is actually being saved somewhere accessible to the rule. I'm attaching a screenshot of my rules download settings. You might try my settings and try loading the file from the created variable and displaying a message of the file id to see if that gets you any further. If you can get the file, i think its just a matter of tweaking the rule from there. If you cannot, I'm not sure whats going on for you.

thoriax’s picture

@apmsooner
Thanks to be so kind;
I was able to fix it based on the screenshot you provided!

I resolved it by enabling the cookie ;)

SEND COOKIE?
Sends the cookie information as sent by the browser to the request. This is useful if you want the request to use the session information, such as the logged in user.

I will look into the docraptor. It seems to be a nice module with more flexibility. Nice to know this one..

Many thanks!