Generate pdf with form submission data I'm using amforms for a client and need to create a user confirmation email that has a pdf attached containing the submitted form data, like a receipt.

After a little google searching, I believe I'd have to hook into amforms' onEmailSubmission method and generate the pdf using the mPDF library, is this correct?


How do I add the mPDF library to Craft?
After creating a nice pdf template design, how can I populate a 2 column table in the pdf with form names/values submission data from all the fields?
And then finally attach that pdf to the user confirmation email?


Does anybody have experience with this whether using amforms or not?
Note: I've never used amforms nor mPDF, so I'm on a bit thin ice with this answer – hopefully it'll prove semi-useful, though.


  How do I add the mPDF library to Craft?


To include any third-party PHP library in Craft, you'll need to build a custom plugin, since that's the only real way to write (or include) PHP inside a Craft CMS app.

Craft plugins are just plain ol' PHP, so to actually include the library, you can use a regular require statement:

public function init()
{
    ...
    require_once('/path/to/mpdf.php');
}


...however, the recommended way to include a third party package is to use Composer (which mPDF supports) and autoload your dependencies from your plugin's main class' init method:

public function init()
{
    ...
    require_once(__DIR__ . '/vendor/autoload.php');
}


One thing to pay attention to when using third party libraries inside a Craft plugin, is that Craft has it's \Craft namespace for the core and all plugins, and your third party library's code won't be found under that namespace. To account for this, you can use the use statement (add it to the top of any file where you want to use the mPDF library:

use \Mpdf\Mpdf as Mpdf;

...
$mpdf = new Mpdf();


...or you can skip the use statement, in which case you'll have to type out the namespace path for the third party library every time you use it:

$mpdf = new \Mpdf\Mpdf();


  After creating a nice pdf template design, how can I populate a 2 column table in the pdf with form names/values submission data from all the fields?


In order to generate a PDF based on the fields submitted via amforms, you'll probably need to hook into the onEmailSubmission event (as you mention), and render a Twig template to HTML before using mPDF to generate a PDF from that HTML output.

Here's a very basic example – this would also go into your plugin's main class' init method:

public function init()
{
    // Require your Composer dependencies (mPDF, possibly others)
    require_once(__DIR__ . '/vendor/autoload.php');

    // Hook into amforms' onEmailSubmission event
    craft()-&gt;on('amForms_submissions.onEmailSubmission', function (Event $event) {

        $params = $event-&gt;params();

        // Use whatever's in $params to generate a PDF via mPDF
        // One way to do it is to compile a Twig template from your site's /templates folder via Craft's [TemplatesService][5], and then feed the HTML output to mPDF:
        $html = craft()-&gt;templates-&gt;render('/path/to/template', $params);

        // Create a mPDF instance, and generate a PDF file from the HTML
        $mpdf = new Mpdf();
        $mpdf-&gt;WriteHTML($html);

        // Have mPDF save the PDF file to some location
        $mpdf-&gt;Output('/path/to/your/pdf.pdf');

        // Some code that will attach the PDF to the email amforms will send
        ...
    });
}


Note: As I've never used amforms, I'm not totally sure about the syntax for the actual event listener here – the plugin's documentation is lacking in terms of its custom events; briefly looking at the source code makes me believe that amForms_submissions is the correct prefix for the event, but a minor disclaimer here.

Beyond the simple example above, for details on how to generate the actual PDF, I'll have to refer to the official mPDF documentation: https://mpdf.github.io/.


  And then finally attach that pdf to the user confirmation email?


I've no idea how to attach the PDF to the actual confirmation email amforms sends, and the documentation doesn't mention a use case like that – hopefully someone familiar with amforms can help you out with that part.

Finally – this is probably not helpful to your specific use case, but still – here's how you could send the PDF using Craft's EmailService:

$email = new EmailModel();
$email-&gt;toEmail = 'hello@example.com';
$email-&gt;subject = 'Your PDF is ready, boss';
$email-&gt;body    = 'How you doin, {{ user.name }}?';
$email-&gt;attachments = [[
   'path' =&gt; '/path/to/your/pdf.pdf',
   'name' =&gt; 'An awesome PDF',
   'encoding' =&gt; 'UTF-8',
   'type' =&gt; 'application/pdf',
]];

craft()-&gt;email-&gt;sendEmail($email);


(If amforms doesn't offer a way to include the PDF attachment, you might be able to solve it by a) using the onBeforeEmailSubmission event instead and setting $event-&gt;performAction to false inside the event handler, to prevent amforms from actually sending the email confirmation; then compile an EmailModel yourself (like in the example above), and sending that instead – just an idea)