Gererating the pdf and sending it as an email attachment in drupal
rajan — Sun, 11/14/2010 - 21:46
Dompdf can be use to generate the pdf more easily, I looked at dompdf module in drupal which can be use with views.
Since we can generate the pdf for our custom drupal form very easily using dompdf but generating the pdf of form and sending it as an email attachment seems very interesting.
Here is my a sample module provides an example of creating the pdf of the drupal form and allow to download as well as allow to send the pdf as an email attachment using mimemail.
Sample form may be as follow
function pdf_maker_sample_form( $form_state ) { '#type' => 'fieldset', '#title' => 'PDF Sample Form' ); '#type' => 'textfield', '#required' => TRUE, '#size' => 25 ); '#type' => 'textfield', '#required' => TRUE, '#size' => 25 ); '#type' => 'textarea', ); // for directly download the pdf $form['submit_download'] = '#type' => 'submit', '#value' => 'Download PDF', ); // for sending pdf as an email attachement $form['submit_mail'] = '#type' => 'submit', '#value' => 'Email Me PDF', ); return $form; }
Submit hook of pdf_maker_sample_form.
For sending pdf with email attachment, I have taken support of mimemail module, which is easy to use for sending email with attachment.
function pdf_maker_sample_form_submit( $form, &$form_state ) { // need to include dompdf library, // considering module name is pdf_maker and dompdf library within the module. // check the library files are exist or not. return; } $values = $form_state['values']; // build the html from submitted values of form $output = "<html> <head> <style type='text/css'> </style> </head> <body> <p><b> Sample PDF form </b></p> <table class='' cellspacing='0' border='1' width='500' height='300'> "; $output .= "<tr><td width=150>Contact Name</td><td>{$values['name']}</td></tr>"; $output .= "<tr><td width=150>Email</td><td>{$values['email']}</td></tr>"; $output .= "<tr><td>Address</td><td>{$values['address']}</td></tr>"; } $output .= "</table> </body> </html>"; // include library file // generate an pdf of html output using dompdf. $dompdf = new DOMPDF(); $dompdf->load_html($output); $dompdf->render(); // for Download the pdf if ( $values['op'] == 'Download PDF' ) { $dompdf->stream('sampleForm.pdf'); return; } // to gerenate pdf and sending it as an email attachment. // create an empty file within drupals file directory $htmlData = $dompdf->output(); // write the pdf content within that file. file_put_contents($pdfPath, $htmlData); $send = "rajan.mayekar@gmail.com"; // email body 'filepath' => $pdfPath, 'filemime' => 'mime/type', 'filename' => $pdfName, 'list' => TRUE, ); // sending email with attachments, // with using mimemail for sending email with attachments ( mimemail module ) mimemail($send, $values['email'], "Sample pdf gerenated for drupal form", $body, NULL, array( ), NULL, $attachments); drupal_set_message( t('Email has been sent to %1 with pdf of current form as an attachment.', array( '%1' => $values['email'] ) ) ); }


