Rajan Mayekar

  • home
  • gallery
  • about
  • contact
Home

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

  1. function pdf_maker_sample_form( $form_state )
  2. {
  3.  
  4. $form['pdf'] = array(
  5. '#type' => 'fieldset',
  6. '#title' => 'PDF Sample Form'
  7. );
  8.  
  9. $form['pdf']['email'] = array(
  10. '#title' => t('Contact Email'),
  11. '#type' => 'textfield',
  12. '#required' => TRUE,
  13. '#size' => 25
  14. );
  15.  
  16. $form['pdf']['name'] = array(
  17. '#title' => t('Contact Name'),
  18. '#type' => 'textfield',
  19. '#required' => TRUE,
  20. '#size' => 25
  21. );
  22.  
  23. $form['pdf']['address'] = array(
  24. '#title' => t('Address'),
  25. '#type' => 'textarea',
  26. );
  27.  
  28. // for directly download the pdf
  29. $form['submit_download'] =
  30. array(
  31. '#type' => 'submit',
  32. '#value' => 'Download PDF',
  33. );
  34.  
  35. // for sending pdf as an email attachement
  36. $form['submit_mail'] =
  37. array(
  38. '#type' => 'submit',
  39. '#value' => 'Email Me PDF',
  40. );
  41.  
  42. return $form;
  43. }

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.

  1. function pdf_maker_sample_form_submit( $form, &$form_state ) {
  2.  
  3. // need to include dompdf library,
  4. // considering module name is pdf_maker and dompdf library within the module.
  5. // check the library files are exist or not.
  6. if ( !file_exists( drupal_get_path('module', 'pdf_maker') . '/dompdf/dompdf_config.inc.php' ) ) {
  7. drupal_set_message( t('Dompdf did not found!') );
  8. return;
  9. }
  10.  
  11. $values = $form_state['values'];
  12.  
  13. // build the html from submitted values of form
  14. $output = "<html>
  15. <head>
  16. <style type='text/css'>
  17. </style>
  18. </head>
  19. <body>
  20. <p><b> Sample PDF form </b></p>
  21. <table class='' cellspacing='0' border='1' width='500' height='300'> ";
  22.  
  23. $output .= "<tr><td width=150>Contact Name</td><td>{$values['name']}</td></tr>";
  24. $output .= "<tr><td width=150>Email</td><td>{$values['email']}</td></tr>";
  25.  
  26. if ( trim($values['address']) ) {
  27. $output .= "<tr><td>Address</td><td>{$values['address']}</td></tr>";
  28. }
  29.  
  30. $output .= "</table>
  31. </body>
  32. </html>";
  33.  
  34. // include library file
  35. require_once drupal_get_path('module', 'pdf_maker') . '/dompdf/dompdf_config.inc.php';
  36.  
  37. // generate an pdf of html output using dompdf.
  38. $dompdf = new DOMPDF();
  39. $dompdf->load_html($output);
  40. $dompdf->render();
  41.  
  42. // for Download the pdf
  43. if ( $values['op'] == 'Download PDF' ) {
  44. $dompdf->stream('sampleForm.pdf');
  45. return;
  46. }
  47.  
  48. // to gerenate pdf and sending it as an email attachment.
  49.  
  50. // create an empty file within drupals file directory
  51. $pdfName = 'sampleform_'.rand(97,122).'.pdf';
  52. $pdfPath = file_create_filename( $pdfName, file_directory_path() );
  53.  
  54. $htmlData = $dompdf->output();
  55.  
  56. // write the pdf content within that file.
  57. file_put_contents($pdfPath, $htmlData);
  58.  
  59. $send = "rajan.mayekar@gmail.com";
  60.  
  61. // email body
  62. $body = "This is an test email send from ". base_path() ." with a sample pdf as an attachment.";
  63.  
  64. $attachments[] = array(
  65. 'filepath' => $pdfPath,
  66. 'filemime' => 'mime/type',
  67. 'filename' => $pdfName,
  68. 'list' => TRUE,
  69. );
  70.  
  71. // sending email with attachments,
  72. // with using mimemail for sending email with attachments ( mimemail module )
  73. mimemail($send, $values['email'], "Sample pdf gerenated for drupal form", $body, NULL, array( ), NULL, $attachments);
  74. drupal_set_message( t('Email has been sent to %1 with pdf of current form as an attachment.', array( '%1' => $values['email'] ) ) );
  75.  
  76. }

  • dompdf
  • drupal

Monthly archive

  • April 2010 (6)
  • May 2010 (3)
  • June 2010 (1)
  • July 2010 (2)
  • August 2010 (1)
  • September 2010 (2)
  • October 2010 (2)
  • November 2010 (1)
  • December 2010 (1)
  • March 2011 (2)
  • 1
  • 2
  • next ›
  • last »

Tags

ahah bookmarks cck civicrm civireport css datatable dompdf drupal flash chart formapi IE jquery module modules mysql panels php theming tricks UI views

Find me on

  


Ohloh profile for rajanmayekar

Meta

  • Login
  • Register
  • Drupal
  • Civicrm
  • home
  • gallery
  • about
  • contact