Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Show "Save as PDF file" button when survey ends - can I implement MPDF?

  • Sweden
  • Sweden's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
9 years 1 month ago #116993 by Sweden
I tried to post on an old thread but I should probably have started my own thread instead (my request is a bit different).

Okay, so I found this comment:

tammo wrote:
since the PDF output from LimeSurvey is a bit hard to alter, we use MPDF implemented in the backend. This works great, but you have to do some styling yourself.

www.mpdf1.com/mpdf/index.php


Very interesting so I send a private message to Tammo (I might as well post my question here so other people can benefit from any answers I might receive. And maybe someone else knows the answer to my question).

So here is my question:

How can I implement MPDF (PDF file generator) so it can create a PDF file at the end of the survey? My goal is that the survey participants answer a couple of questions and based on their answers they will receive some advice when they click the NEXT button (for that I use "Text display" as question type including "Relevance equation"). I would like to have a "Save this page as PDF" button on that page. MPDF looks great, but I haven't got a clue how to make it work. I have used a couple of days trying to figure out how I can make it work, but I really don't know much about php etc. How do you send the php to MPDF (can it use the content in a div and then make a pdf out of it??). Also, where do I insert code without creating a big security hole?

Any help would be appreciated - even a few hints and tips would be a great help. Thanks.

PS: The code below is something I found while searching for a solution, but I don't know how to customize it so it will work.
Code:
<?php
$content = get_include_contents('??????');
include('./mpdf/mpdf.php');
$mpdf=new mPDF();
$mpdf->WriteHTML($content);
$mpdf->Output(example.pdf,'D');   exit;
 ?>
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 1 month ago #117016 by DenisChenu
Hi,

Late, i know .... but in a plugin, i use this to include third party Class file
Code:
        Yii::setPathOfAlias('myPlugin', $basedir);
        Yii::import('myPlugin.third_party.thirdPartyClass');
        $newClass = new thirdPartyClass();
The plugin directory is like this:
Code:
myPlugin/myPlugin.php
myPlugin/third_party.thirdPartyClass.php

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The following user(s) said Thank You: Sweden
The topic has been locked.
  • Sweden
  • Sweden's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
9 years 1 month ago #117084 by Sweden
Hi Denis,

Thanks for your help but I'm not sure how your code can make MPDF work... I'm not that clever.

Hmmm it probably isn't as easy as I thought it would be. Too bad I haven't got any response from Tammo, but maybe he doesn't use it anymore or something?
The topic has been locked.
More
9 years 1 month ago - 9 years 1 month ago #117332 by stefanverweij
Hi Sweden, I use mpdf for my reports as well, but I use remote control, but maybe I can help.

I think you would need to create an external php file where you create the report, using the variables that you need for the report from (i'm guessing) a POST request from your previous page.

Easiest way then is to buffer the html output created by the php file with ob_start(); see mpdf1.com/manual/index.php?tid=435 )

So your code would be something like this:
( not sure about the post thing, I have no idea regarding security here)
Code:
<?php
$advice = $_POST["advice"];
include('./mpdf/mpdf.php');
$mpdf=new mPDF();
ob_start();
 
echo '<div>You should follow our ' . $advice . ' course</div>';
 
$html = ob_get_contents();
ob_end_clean();
$mpdf->WriteHTML($html);
$mpdf->Output(example.pdf,'D');   exit;
 ?>
 

Be sure to keep an eye on the single and double quotation marks in your php/html, and make sure you close your html tags. Hope this helps,
Stefan
ps if anyone with more php experience has better ideas on how to pass the data please chip in :)
Last edit: 9 years 1 month ago by stefanverweij. Reason: fixed code
The following user(s) said Thank You: DenisChenu, Sweden
The topic has been locked.
  • Sweden
  • Sweden's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
9 years 1 month ago #117340 by Sweden
Hi Stefan, thanks very much.

I think it will be a great help. POST is more secure than GET and it should be secure enough for my purpose.

I need external URLs to be active (clickable) in the PDF - not sure if MPDF can handle this... Also, images should display without problems. Some PDF generators have problems with images.

I will see if I can make it work, but it would be nice if this could be a part of LimeSurvey.

Thanks again ;)
The topic has been locked.
More
9 years 1 month ago #117350 by stefanverweij
mpdf handles clickable links without a problem, just add a <a href> :) Images should work without a problem as well.

Good luck, let me know if you run into any problems,

Stefan
The following user(s) said Thank You: Sweden
The topic has been locked.
  • Sweden
  • Sweden's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
9 years 1 month ago #117353 by Sweden
Thanks, that sounds great! I haven't had that much time to look at it, but can't wait to see the first generated PDF :)
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose