Reputation: 173
I've been trying to do some research on this but can't find a tutorial which explains how to do what I'm wanting to.
I'm wanting to find a way to be able to convert a html form(once user has filled it in and hit submit) into a pdf and then for that pdf to be sent to the appropriate person.
Take this form for example:
<form id="contact-form" method="post" action="contact.php">
<input name="name" type="text" id="name" class="form-control" placeholder="Your name" required><br>
<input name="email" type="email" id="email" class="form-control" placeholder="Your email" required><br>
<textarea name="message" id="message" class="form-control" placeholder="Message" row="4" required></textarea><br>
<input type="submit" class="form-control submit" value="SEND MESSAGE">
</form>
and this is my current php for it:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'C:\PHPMailer\PHPMailer-master\src\Exception.php';
require 'C:\PHPMailer\PHPMailer-master\src\PHPMailer.php';
require 'C:\PHPMailer\PHPMailer-master\src\SMTP.php';
$mail = new PHPMailer(TRUE);
try {
$mail->isHTML(true);
$mail->setFrom('[email protected]', 'Contact Form');
$mail->addAddress('[email protected]', 'Lewis');
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = 'tls';
$mail->Username = '*******';
$mail->Password = '*****';
$mail->Port = 587;
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
$mail->Subject = 'contact form';
$mail->isHTML(true);
$mail->Body = "There has been a message sent from our contact form from: $name
<br>This is their message: $message
<br>Please respond to them here: $email";
//Send the message, check for errors
if (!$mail->send()) {
//The reason for failing to send will be in $mail->ErrorInfo
$msg = 'Sorry, something went wrong. Please try again later.';
} else {
header("Refresh:3; url=index.php#contact");
echo "Message sent! Thanks for contacting us. We aim to respond within 1 working day";
}
} else {
$msg = 'Invalid email address, message ignored.';
}
/* Enable SMTP debug output. */
$mail->SMTPDebug = 4;
// $mail->send();
}
catch (Exception $e)
{
echo $e->errorMessage();
}
catch (\Exception $e)
{
echo $e->getMessage();
}
What would I need to change so that the form becomes a pdf and that is what is sent in the email instead?
Thanks
Upvotes: 1
Views: 3564
Reputation: 173
In case anyone else has this issue I'll share my answer on how I've got it working.
First of all I downloaded the fpdf files from http://www.fpdf.org/
Html form obviously stays the same but the php file is now as follows:
<?php
require "fpdf/fpdf.php";
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'C:\PHPMailer\PHPMailer-master\src\Exception.php';
require 'C:\PHPMailer\PHPMailer-master\src\PHPMailer.php';
require 'C:\PHPMailer\PHPMailer-master\src\SMTP.php';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail = new PHPMailer(TRUE);
try {
$mail->isHTML(true);
$mail->setFrom('[email protected]', 'Contact Form');
$mail->addAddress('[email protected]', 'eg');
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = 'tls';
$mail->Username = '********';
$mail->Password = '********';
$mail->Port = 587;
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10, 'Name:');
$pdf->Ln();
$pdf->Cell(40,10, $name);
$pdf->Ln();
$pdf->Cell(40,10, 'Email:');
$pdf->Ln();
$pdf->Cell(40,10, $email);
$pdf->Ln();
$pdf->Cell(40,10, 'Their message:');
$pdf->Ln();
$pdf->Cell(40,10, $message);
$pdfdoc = $pdf->Output('', 'S');
$pdf->Ln();
if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
$mail->Subject = 'contact form';
$mail->isHTML(true);
$mail->Body = "There has been a message sent from our contact form
<br>Details can be found in the attached pdf";
$mail->addStringAttachment($pdfdoc, 'contact.pdf');
//Send the message, check for errors
if (!$mail->send()) {
//The reason for failing to send will be in $mail->ErrorInfo
$msg = 'Sorry, something went wrong. Please try again later.';
} else {
$msg = 'Message sent! Thanks for contacting us.';
header("Refresh:3; url=index.php#contact");
echo "Message sent! Thanks for contacting us. We aim to respond within 1 working day";
}
} else {
$msg = 'Invalid email address, message ignored.';
}
/* Enable SMTP debug output. */
$mail->SMTPDebug = 4;
// $mail->send();
}
catch (Exception $e)
{
echo $e->errorMessage();
}
catch (\Exception $e)
{
echo $e->getMessage();
}
?>
This is a straightforward pdf and not much to it but point is that it works and I think is a good starting point for someone who may struggle with this
Upvotes: 3
Reputation: 106
From my understanding. You cant just turn html into a pdf. You will need to build a TCPDF view of the form. Then combine the data and the form into the pdf which you then should encode to base64 to attach it to an email.
So for the first part create a php file with your TCPDF form.
<?php
class Contactform {
public function generate($ctrl, $data) {
$ctrl->pdf->SetCreator(PDF_CREATOR);}
;?>
Then generate the form by combining the TCPDF view and the data to write into it.
In Codeigniter I do this
$this->load->library(["pdf", "forms/contactform"]);
$form = $this->contactform->generate($this, $data);
$attachment = chunk_split(base64_encode($form));
$separator = md5(time());
$message .= "--" . $separator . PHP_EOL;
$message .= "Content-Type: application/octet-stream; name=\"contact_form.pdf\"" . PHP_EOL;
$message .= "Content-Transfer-Encoding: base64" . PHP_EOL;
$message .= "Content-Disposition: attachment" . PHP_EOL . PHP_EOL;
$message .= $attachment . PHP_EOL . PHP_EOL;
$message .= "--" . $separator . "--";
You have to set the Content type when including an attachment
Upvotes: 2