Convert html form to pdf and email using phpmailer
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('example@hotmail.co.uk', 'Contact Form');
$mail->addAddress('example@hotmail.co.uk', '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
Submit html form to PDF without database
you can try this
$("#myform input:text").each(function(){
var val = $(this).val();
$(this).attr("value", val);
});
var form = $("#myform").html();
alert(form);
FIDDLE DEMO
Submit HTML form to PDF
Update April 11, 2013:
Since posting this question I have been utilizing FPDF on multiple projects where I needed to accomplish this goal. Although it cannot seem to "merge" template PDFs with the provided data, it can create the PDF from scratch.
One example I have used, I had a high resolution PNG for printing (similar to initial question) which we had to write the customer's name and today's date clearly in the center. I simply made the background of the PDF using FPDF->Image() and write the text afterwards using FPDF->Text().
It was very simple after all, you will need to look up the paper sizes to determine the X,Y,W,H of the image and then base your text fields relative to those numbers.
There was even a Form Filling extension, but I couldn't get it to work.
It seems as though I should answer my own question, although Visions answer may be better (seems to be deleted?). I used Vasiliy Faronov's link which was a comment to my main question: https://stackoverflow.com/a/1890835/200445
Here I found how to install pdftk and run a command to merge (flatten) my FDF and PDF files. I still used the "hacky" way to generate an FDF using Koivi's FDF Generator but it works for the most part.
One caveat is that some characters, like single and double quotes are not inserted correctly. It may be an issue of escaping the fields, but I could not find an answer.
Regardless, my PDF form generator is working, but anyone with a similar issue should look for a better solution.
Convert HTML with inline PHP to PDF
This is a basic example of how your could would look like if you were using TCPDF
<?php
$pdf = new TCPDF(PDF_PAGE_ORIENTATION_L, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
//TCPDF_INIT_CODE_GOES_HERE
// ...
$html = '<!DOCTYPE HTML>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js">
</script>
<!-- allow login page to link to this when user is an admin -->
<title>Report</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<link rel="manifest" href="/manifest.json">
<center><img src="icons/sbuxLogo.png" width="100" height="100px"></center>
</br></br>
<center><h1>Welcome Administrator!</h1></center>
</br></br>
<center>
<div id="report">
<h2>Report:</h2>
</br>
</center>';
require "phpScripts/connect.php";
$startDate = $_GET['startDate'];
$endDate = $_GET['endDate'];
$sql = "SELECT * FROM orderlist WHERE NOT (dateOrdered < '$startDate' OR dateOrdered > '$endDate');";
$result = $conn->query($sql);
$html .= "<center>";
$html .= "<ul>";
if($result->num_rows >0)
{
$i = 0;
while($row = $result->fetch_assoc()) // this loads selected drinks via date
into ul
{
$html .= "<li style=
'margin-left: 0;
list-style-type: none;
color: #727272;
height: 40px;
font-size: 15px;
font-family: Open Sans;
border-style: none;
width: 50%;
list-style-type: none;
margin:0;
padding-bottom:0px;
padding:0;
overflow: hidden;'>
Drink: ".$row["drinkName"]. ", Date Ordered: " .$row["dateOrdered"] . ",
Cost: " .$row["drinkCost"] . "</li>";
$i = $i+1;
}
}else {
$html .= "<p> No orders found. </p>";
}
$html .= "</ul>";
$html .= "</center>";
$html .= '</div>';
$pdf->writeHTML($html);
$pdf->Output('TITLE_OF_PDF_FILE.pdf', 'I'); // I flag means it will display the PDF in a browser
Related Topics
Query With Multiple Values in a Column
How to Change the Background Color of Dropdown List in Select Tag
How to Check If MySQL Results Returned Empty in PHP
How to Remove the Extra Square Bracket from Json Having Multiple Arrays
How to Get Data from Another Table Using Id from Another Table
How to Count Columns With the Same Value in a Specific Row in MySQL
How to Echo Selected Value of Dropdown in PHP
Php Warning: Mysqli_Connect(): (Hy000/2002): Connection Refused
How to Add a Space Between Every Sequence of Four Characters (Like a Credit Card Number)
Passing an Array to a Query Using a Where Clause
How to Generate Unique Random Value for Each User in Laravel and Add It to Database
Tell Composer to Use Different PHP Version
The Get Method Is Not Supported for This Route. Supported Methods: Put. But I'M Using Put
Php 7 With Phpmyadmin Gives Lots of Deprecation Notices
Sending Message in Telegram Bot With Images
Reading Numbers as Text Format With Phpexcel