Allow user to return to assessment results and/or create pdf from results

More
10 months 3 weeks ago #135148 by mrli999

mattheoh wrote: Just to let you know , I managed to generate automatically a PDF of the assessment, whicj is compiled when the assessment page is displayed. I used the library HTML2PDF.
Also added a button to send, the PDF by mail.
The name of the PDF is : INFOX-IDSURVEY.PDF which permits to add it an equation question to display it in the board.
if someone needs the script I can post it here.
Thanks again for the help.


Could you please post it here? Thanks.

Please Log in to join the conversation.

More
10 months 3 weeks ago - 10 months 3 weeks ago #135207 by mattheoh

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)
<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/)
<?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
<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&eacute;t&eacute; : <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&eacute;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&eacute;alis&eacute; 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
<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
 
<?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&eacute;t&eacute; $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.
Last Edit: 10 months 3 weeks ago by mattheoh.
The following user(s) said Thank You: coeneisma, mrli999, isaak78

Please Log in to join the conversation.

More
10 months 3 weeks ago #135244 by jaws40
Could you show the script Please .? In eed something lime iT for a project

Please Log in to join the conversation.

More
10 months 3 weeks ago #135245 by tammo
Hi Jaws, it appears right above your most recent question...


Tammo ter Hark
Tools for Research
Haarlem/Amsterdam, The Netherlands
The following user(s) said Thank You: mattheoh

Please Log in to join the conversation.

More
9 months 2 weeks ago - 9 months 2 weeks ago #137471 by mlyter
Sorry I'm very late to the party, but in case it may help folks in the future:

The data that I was dealing with was not sensitive so I used the End URL feature of the LimeSurvey to send the user to a custom PHP page that included the {SAVEDID} and the {SID} to allow the user to view his/her results. My End URL looked something like this example.com/results/index.php?user={SAVEDID}&asmt={SID } . With this method I think a pattern would be pretty simple to figure out, so it's probably no good if you're dealing with sensitive information.

When creating my custom PHP results page, I was able to access the questions and answers to display the results in the fashion that I wanted by connecting the the mysql database using the {SAVEDID} and {SID} as identifiers and then retrieving the results that I wanted.

At the time I was doing this project I couldn't seem to get a PDF library (a free one anyhow) to work properly, so I opted to use the free service provided at printfriendly.com (which also allows you to mail the PDF) . Part of my custom PHP page included a link directly back to the user's results, including the {SAVEDID} and the {SID} so after the user saved the PDF he or she could view the results in PDF format, or click the link to view their results directly on the PHP page.
Last Edit: 9 months 2 weeks ago by DenisChenu. Reason: Usage of example.com

Please Log in to join the conversation.