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
Comment #1
ob3ron commentedI 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.
Comment #2
jcnventuraHi,
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).
Comment #3
ob3ron commentedHi 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!
Comment #4
jcnventuraarvana, 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..
Comment #5
ob3ron commentedHi 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?
Comment #6
ob3ron commentedOK, 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:
Can I suggest that this be added in to the module documentation?
Comment #7
jcnventuraThat would fit really well into http://drupal.org/node/190173 !!
Can you add that yourself, please? :)
Comment #8
jcnventuraAdded it to the FAQ.
Comment #9
asghar commentedHI...
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. ?
Comment #10
jcnventuraIt's in the print_pdf/print_pdf.pages.inc file.. You need to include it explicitly before using it.
Comment #11
pieterdc@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.
Comment #12
ryanfc78 commentedWhat 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.
Comment #13
liminu commentedHow can i change the sender email?Solve, use the dev module
Comment #14
cjsmith87 commentedThe 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?
Comment #15
jimmyyy commentedprint_pdf function can be called to create PDF doc file. More other functions?