954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Email form results as PDF

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.

Roebuc
Junior Poster
136 posts since Nov 2007
Reputation Points: 30
Solved Threads: 2
 

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
// download fpdf class (http://fpdf.org)
require("fpdf153/fpdf.php");

// fpdf object
$pdf=new FPDF();

// generate a simple PDF (for more info, see http://fpdf.org/en/tutorial/)
$pdf->AddPage();
$pdf->SetFont("Arial","B",14);
$pdf->Cell(40,10, "this is a pdf example");

// email stuff
$to = "target@domain.com"; 
$from = "me@domain.com"; 
$subject = "send email with pdf attachment"; 
$message = "Please see the attachment.";

// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (we use PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "example.pdf";

// encode data (put attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));


// main header (multipart mandatory)
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol; 
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;

// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$headers .= $message.$eol.$eol;

// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";

// send message
mail($to, $subject, "", $headers);

?>
martin5211
Posting Whiz in Training
271 posts since Aug 2007
Reputation Points: 52
Solved Threads: 23
 

yeah,you can move that to the snippets page.It will be useful to many!

ryan_vietnow
Posting Pro
578 posts since Aug 2007
Reputation Points: 28
Solved Threads: 71
 

I've added the source at code snippets, using HTML message format at this time.

martin5211
Posting Whiz in Training
271 posts since Aug 2007
Reputation Points: 52
Solved Threads: 23
 

Martin, thank you. That is perfect!

Roebuc
Junior Poster
136 posts since Nov 2007
Reputation Points: 30
Solved Threads: 2
 

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

Nandkishore354
Newbie Poster
1 post since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You