mrli999 wrote: Could you please post it here? Thanks.
Sure,
1. JS code to add to assessment.tpl (to call the php script and send the datas - theses are my datas, you have to adapt it of course)
Code:
<script type="text/javascript" charset="utf-8">
$(window).load(function(){
$(document).ready(function() {
var content = $("#canvas").html();
var fichier = "{INFO1}-{SAVEDID}";
var societe = "{INFO1}";
var codepostal = "{INFO2}";
var ville = "{INFO3}";
var personne = "{INFO4}";
var fonction = "{INFO5}";
var personnemail = "{INFO6}";
var expert = "{TOKEN:FIRSTNAME} {TOKEN:LASTNAME}";
var expertmail = "{TOKEN:EMAIL}";
$.ajax({
url: "/upload/html2pdf/pdf_engine.php",
method: "post",
data: { filename: fichier, data: content, company: societe, cp: codepostal, town: ville, person: personne, job: fonction, persmail: personnemail, exp: expert, expmail: expertmail },
});
});
});
</script>
2. Download HTML2PDF here :
html2pdf.fr/download
(for information I took 4.4 version , cause 4.5 needs compozer to be installed)
3. The pdf_engine.php script to generate the pdf with HTML2PDF (I put it in upload/html2pdf/)
Code:
<?php
/**
* HTML2PDF Library - example
*
* HTML => PDF convertor
* distributed under the LGPL License
*
* @package Html2pdf
* @author Laurent MINGUET <webmaster@html2pdf.fr>
* @copyright 2016 Laurent MINGUET
*
* isset($_GET['vuehtml']) is not mandatory
* it allow to display the result in the HTML format
*/
require_once(dirname(__FILE__).'/html2pdf.class.php');
// get the HTML
ob_start();
// get the filename and data from POST
$filename = $_POST["filename"];
$data = $_POST["data"];
$societe = $_POST["company"];
$codepostal = $_POST["cp"];
$ville = $_POST["town"];
$personne = $_POST["person"];
$fonction = $_POST["job"];
$personnemail = $_POST["persmail"];
$expert = $_POST["exp"];
$expertmail = $_POST["expmail"];
include(dirname(__FILE__).'/modele-rapport-pdf.php');
$content = ob_get_clean();
try
{
// init HTML2PDF
$html2pdf = new HTML2PDF('P', 'A4', 'fr', true, 'UTF-8', array(0, 0, 0, 0));
// display the full page
//$html2pdf->pdf->SetDisplayMode('fullpage');
// convert
$html2pdf->writeHTML($content, isset($_GET['vuehtml']));
// add the automatic index
//$html2pdf->createIndex('Sommaire', 30, 12, false, true, 2);
// send the document to the browser and force the download
$path = "/var/www/mydomaine/website/upload/pdf/";
$filename = $filename . ".pdf";
$html2pdf->Output($path . $filename, "F");
// return the filename to ajax request
echo $filename;
}
catch(HTML2PDF_exception $e) {
echo $e;
exit;
}
4. Then create your model - I put the file here upload/html2pdf/modele-rapport-pdf.php - You can look at this page to customize your own model :
html2pdf.fr/example
Code:
<style type="text/css">
<!--
table.page_header {width: 100%; border: none; background-color: #c7d4d9; border-bottom: solid 2px #2c3e50; padding: 1mm; font-size: 9pt;font-family: Helvetica}
table.page_footer {width: 100%; border: none; background-color: #c7d4d9; border-top: solid 2px #2c3e50; padding: 1mm;font-size: 7pt; font-family: Helvetica}
div.content {text-align: left; font-size: 11px; font-family: Helvetica; padding: 3mm; width: 95%;}
ul { width: 95%; list-style-type: square; }
ul li { padding-bottom: 1mm; }
h1 { text-align: left; font-size: 15px}
h3 { text-align: left; font-size: 13px}
-->
</style>
<page backtop="3mm" backbottom="3mm" backleft="3mm" backright="3mm">
<table class="page_header">
<tr>
<td style="width: 50%; text-align: left">
<p style="font-size: 12pt; font-weight: bold;">Rapport</p>
<ul>
<li>Date : <b><?php echo date('d-m-Y'); ?></b></li>
<li>Société : <b><?php echo $societe; ?></b></li>
<li>Code Postal : <b><?php echo $codepostal; ?></b></li>
<li>Ville : <b><?php echo $ville; ?></b></li>
<li>Personne interrogée : <b><?php echo $personne; ?></b></li>
<li>Fonction : <b><?php echo $fonction; ?></b></li>
<li>Email : <b><?php echo $personnemail; ?></b></li>
<li>Expert BPF Cosmed : <b><?php echo $expert; ?></b></li>
<li>Email expert : <b><?php echo $expertmail; ?></b></li>
</ul>
</td>
<td style="width: 50%; text-align: right">
<img src="./logo-cosmed.png" alt="Logo HTML2PDF" style="width: 200px">
</td>
</tr>
</table>
<div class="content">
<?php echo $data; ?>
</div>
<page_footer>
<table class="page_footer">
<tr>
<td style="width: 50%; text-align: left;">
Rapport<b><?php echo $societe; ?></b>
</td>
<td style="width: 50%; text-align: right">
Réalisé par <b><?php echo $expert; ?></b> le <?php echo date('d-m-Y'); ?>
</td>
</tr>
</table>
</page_footer>
</page>
And that's it , a pdf will be generated automatically when the assessment page is loaded. The pdf file will be saved in upload/pdf folder
I also add 2 buttons in the assessment page :
A. a button to download the pdf file
B. a button to send the file by mail :
1. JS to insert in assessment.pstpl for sending mail with attached pdf
Code:
<div style="text-align: right">
<a href="/upload/pdf/{INFO1}-{SAVEDID}.pdf" download="{INFO1}-{SAVEDID}.pdf" class="btn btn-default">Télécharger le rapport</a>
<div id="sendmail" class="btn btn-default">Envoyer fichier PDF</div>
<div id="success" style="display: none; color: #18bc9c;"> Fichier bien envoyé !</div>
</div>
<script type="text/javascript" charset="utf-8">
$(function() {
$("#sendmail").click(function() {
var email = "{TOKEN:EMAIL}";
var societe = "{INFO1}";
var fichier = "{INFO1}-{SAVEDID}.pdf";
$('#success').show().delay(5000).fadeOut();
$.ajax({
type: "POST",
url: "/upload/html2pdf/sendmail.php",
data: { to: email, filename: fichier, company: societe },
});
return false;
});
});
</script>
2. Php script : upload/html2pdf/sendmail.php
Code:
<?php
$societe = $_POST['company'];
$email_to = $_POST['to']; // The email you are sending to (example)
$email_from = "your@email.com"; // The email you are sending from (example)
$email_subject = "Rapport - $societe"; // The Subject of the email
$email_txt = "Vous trouverez ci-joint le rapport pour la société $societe"; // Message that the email has in it
$fileatt_name = $_POST['filename']; // Filename that will be used for the file as the attachment
$fileatt = "/var/www/yourdomain/website/upload/pdf/$fileatt_name"; // Path to the file (example)
$fileatt_type = "application/pdf"; // File Type
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers="From: $email_from"; // Who the email is from (example)
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $email_txt;
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
$mailsent = mail($email_to,$email_subject,$email_message,$headers);
?>
I m not a developper, but for sure theses codes could be optimized or cleaned.
But if it can help someone... that's great.