Reputation: 110
I had read a lot of answer about "how to attach PDF via PHPMailer from html2canva or jsPDF", but everything I tried didn't work.
For now, I have an HTML page with some input. When the customer fill all of them, he clicks on Send button.
Everything work, except the attachment of the PDF File.
So, I need your help ;)
This is what I have :
JS Script :
html2canvas($("#Div"), {
onrendered: function(canvas) {
//=== Take screenshot
var imgData = canvas.toDataURL('image/jpeg');
//=== PDFize it
var doc = new jsPDF("p", "px");
var options = {
pagesplit: true
};
//=== Split it
var imgWidth = 210;
var pageHeight = 295;
var imgHeight = canvas.height * imgWidth / canvas.width;
var heightLeft = imgHeight;
var doc = new jsPDF('p', 'mm');
var position = 0;
doc.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
while (heightLeft >= 0) {
position = heightLeft - imgHeight;
doc.addPage();
doc.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
}
//=== Send it
$.post("mail.php",
{
data: doc;
}, function () {}).done(function() {/*SOME CODE*/});
//=== Just to test the PDF
doc.save( 'file.pdf');
}
});
And my PHP Script :
<?php
if(!empty($_POST['data']))
{
require 'lib/PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = '*';
$mail->SMTPAuth = *;
$mail->Username = '*';
$mail->Password = '*';
$mail->SMTPSecure = '*';
$mail->Port = *;
$mail->setFrom('*', '*');
$mail->addAddress('*', '*');
//=== Tested this
$originalbase = $base;
//=== or this
$base = explode('data:application/pdf;base64,', $_POST['data']);
$mail->addStringAttachment($originalbase, "name.pdf", "base64", "application/pdf");
$mail->isHTML(true);
$mail->Subject = '*';
$mail->Body = '*';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
} else echo "No Data Found";
?>
I had tested with base64 decode, without, with the mime type, without...
Edit 1: 13.10.2016 Seems to be good with
var PDF = doc.output('datauri');
$base = explode('data:application/pdf;base64,', $_POST['data']);
$base = base64_decode($base[1]);
$mail->addStringAttachment($base, 'pdfName.pdf');
Edit 2: 13.10.2016 Replace
var PDF = doc.output('datauri');
by
var PDF = doc.output('datauristring');
in order to avoid the redirection to the created PDF
Upvotes: 4
Views: 4148
Reputation: 11
//=== Send it
$.post("mail.php",
{
data: doc;
}, function () {}).done(function() {/*SOME CODE*/});
BY
//=== Send it
$.post("mail.php",
{
data: doc //or doc.output('datauristring')
}, function () {}).done(function() {/*SOME CODE*/});
Upvotes: 0
Reputation: 1809
try this line without the other parameters , if it does not work , can you share a part of the begging of the base64 posted to php
$mail->addStringAttachment($pdfString, 'pdfName.pdf');
Upvotes: 3
Reputation: 1809
Use html2pdf , when the user submit the form , send the whole HTML code to php then use this part of the code to create your pdf
$htmlCode= $_POST["htmlCodeSentWithJquery"];
require_once(dirname(__FILE__).'/html2pdf/html2pdf.class.php');
$html2pdf = new HTML2PDF('P','A4','fr');
$html2pdf->WriteHTML($htmlCode);
$html2pdf->Output('pdfFileName.pdf');
More info about the constructor
/**
* Constructeur
*
* @param string $sens - landscape or portrait orientation
* @param string $format - format A4, A5, ...
* @param string $langue - language: fr, en, it ...
* @param boolean $unicode - TRUE means clustering the input text IS unicode (default = true)
* @param String $encoding - charset encoding; Default is UTF-8
* @param array $marges - margins by default, in order (left, top, right, bottom)
* @return null
*/
public function __construct($sens = 'P', $format = 'A4', $langue='en', $unicode=true, $encoding='UTF-8', $marges = array(5, 5, 5, 8))
then send the file using your email function
OR
use the html2pdf email function
$content_PDF = $html2pdf->Output('', true);
require_once(dirname(__FILE__).'/pjmail/pjmail.class.php');
$mail = new PJmail();
$mail->setAllFrom('webmaster@my_site.net', "My personal site");
$mail->addrecipient('mail_user@my_site.net');
$mail->addsubject("Example sending PDF");
$mail->text = "This is an example of sending a PDF file";
$mail->addbinattachement("my_document.pdf", $content_PDF);
$res = $mail->sendmail();
Upvotes: -1