943,685 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 2683
  • PHP RSS
Jul 22nd, 2008
0

Email form results as PDF

Expand Post »
Can someone tell me how I can accomplish this. I want the results of a form to be placed in a pdf and then added as an attachment. I am not sure if this is possible, but if someone knows how, please let me know. Thanks.
Similar Threads
Reputation Points: 30
Solved Threads: 2
Junior Poster
Roebuc is offline Offline
134 posts
since Nov 2007
Jul 23rd, 2008
2

Re: Email form results as PDF

To generate a PDF into PHP, there are two techniques: PDFLib or FPDF. PDFLib has some restrictions on licencing (commercial applications), I encourage to use FPDF because is free and Open Source, although PDFLib is rough and more complete (use your criterion). At http://fpdf.org there are a place to download the class and good tutorials. Use $_GET or $_POST variables to get the form data depending the form method.

To send the email, mail() function or PHPMailer are commons methods to perform these assignments. Please take care to encode the attachment and the headers, set multipart content type first, then the proper content type and encoding in each part, we could send plain or html text and multiple attachments too.

In this example I've used FPDF and mail() techniques. I don't know if it is better move this code to snippets section.

php Syntax (Toggle Plain Text)
  1. <?php
  2. // download fpdf class (http://fpdf.org)
  3. require("fpdf153/fpdf.php");
  4.  
  5. // fpdf object
  6. $pdf=new FPDF();
  7.  
  8. // generate a simple PDF (for more info, see http://fpdf.org/en/tutorial/)
  9. $pdf->AddPage();
  10. $pdf->SetFont("Arial","B",14);
  11. $pdf->Cell(40,10, "this is a pdf example");
  12.  
  13. // email stuff
  14. $to = "target@domain.com";
  15. $from = "me@domain.com";
  16. $subject = "send email with pdf attachment";
  17. $message = "Please see the attachment.";
  18.  
  19. // a random hash will be necessary to send mixed content
  20. $separator = md5(time());
  21.  
  22. // carriage return type (we use PHP end of line constant)
  23. $eol = PHP_EOL;
  24.  
  25. // attachment name
  26. $filename = "example.pdf";
  27.  
  28. // encode data (put attachment in proper format)
  29. $pdfdoc = $pdf->Output("", "S");
  30. $attachment = chunk_split(base64_encode($pdfdoc));
  31.  
  32.  
  33. // main header (multipart mandatory)
  34. $headers = "From: ".$from.$eol;
  35. $headers .= "MIME-Version: 1.0".$eol;
  36. $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
  37. $headers .= "Content-Transfer-Encoding: 7bit".$eol;
  38. $headers .= "This is a MIME encoded message.".$eol.$eol;
  39.  
  40. // message
  41. $headers .= "--".$separator.$eol;
  42. $headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"".$eol;
  43. $headers .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
  44. $headers .= $message.$eol.$eol;
  45.  
  46. // attachment
  47. $headers .= "--".$separator.$eol;
  48. $headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
  49. $headers .= "Content-Transfer-Encoding: base64".$eol;
  50. $headers .= "Content-Disposition: attachment".$eol.$eol;
  51. $headers .= $attachment.$eol.$eol;
  52. $headers .= "--".$separator."--";
  53.  
  54. // send message
  55. mail($to, $subject, "", $headers);
  56.  
  57. ?>
Reputation Points: 52
Solved Threads: 23
Posting Whiz in Training
martin5211 is offline Offline
271 posts
since Aug 2007
Jul 23rd, 2008
0

Re: Email form results as PDF

yeah,you can move that to the snippets page.It will be useful to many!
Reputation Points: 28
Solved Threads: 71
Posting Pro
ryan_vietnow is offline Offline
578 posts
since Aug 2007
Jul 23rd, 2008
0

Re: Email form results as PDF

I've added the source at code snippets, using HTML message format at this time.
Reputation Points: 52
Solved Threads: 23
Posting Whiz in Training
martin5211 is offline Offline
271 posts
since Aug 2007
Jul 23rd, 2008
0

Re: Email form results as PDF

Martin, thank you. That is perfect!
Reputation Points: 30
Solved Threads: 2
Junior Poster
Roebuc is offline Offline
134 posts
since Nov 2007
Sep 30th, 2010
0
Re: Email form results as PDF
This Code Working Awesome. Thnks For great minds. It make my work so simple and save lot of time .
Thanks Once Again

With Regards
Nand
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Nandkishore354 is offline Offline
1 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: If statement problem again...
Next Thread in PHP Forum Timeline: Dynamic checkboxes in Dreamweaver repeating regions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC