Answers.Body: <p>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.</p>
  
  <blockquote>
    <p>How do I add the mPDF library to Craft?</p>
  </blockquote>
  
  <p>To include any third-party PHP library in Craft, you'll need to <a href="http://pluginfactory.io" rel="nofollow noreferrer">build</a> a <a href="https://craftcms.com/docs/plugins/introduction" rel="nofollow noreferrer">custom plugin</a>, since that's the only real way to write (or include) PHP inside a Craft CMS app.</p>
  
  <p>Craft plugins are just plain ol' PHP, so to actually include the library, you can use a regular <code>require</code> statement:</p>
  
  <pre><code>public function init()
  {
      ...
      require_once('/path/to/mpdf.php');
  }
  </code></pre>
  
  <p>...however, the recommended way to include a third party package is to use <a href="https://getcomposer.org/" rel="nofollow noreferrer">Composer</a> (which mPDF <a href="https://github.com/mpdf/mpdf#installation" rel="nofollow noreferrer">supports</a>) and autoload your dependencies from your plugin's main class' <code>init</code> method:</p>
  
  <pre><code>public function init()
  {
      ...
      require_once(__DIR__ . '/vendor/autoload.php');
  }
  </code></pre>
  
  <p>One thing to pay attention to when using third party libraries inside a Craft plugin, is that Craft has it's <code>\Craft</code> 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 <code>use</code> statement (add it to the top of any file where you want to use the mPDF library:</p>
  
  <pre><code>use \Mpdf\Mpdf as Mpdf;
  
  ...
  $mpdf = new Mpdf();
  </code></pre>
  
  <p>...or you can skip the <code>use</code> statement, in which case you'll have to type out the namespace path for the third party library every time you use it:</p>
  
  <p><code>$mpdf = new \Mpdf\Mpdf();</code></p>
  
  <blockquote>
    <p>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?</p>
  </blockquote>
  
  <p>In order to generate a PDF based on the fields submitted via amforms, you'll probably need to hook into the <code>onEmailSubmission</code> event (as you mention), and render a Twig template to HTML before using mPDF to generate a PDF from that HTML output.</p>
  
  <p>Here's a very basic example – this would also go into your plugin's main class' <code>init</code> method:</p>
  
  <pre><code>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
          ...
      });
  }
  </code></pre>
  
  <p>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 <code>amForms_submissions</code> is the correct prefix for the event, but a minor disclaimer here.</p>
  
  <p>Beyond the simple example above, for details on how to generate the actual PDF, I'll have to refer to the official mPDF documentation: <a href="https://mpdf.github.io/" rel="nofollow noreferrer">https://mpdf.github.io/</a>.</p>
  
  <blockquote>
    <p>And then finally attach that pdf to the user confirmation email?</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>Finally – this is probably not helpful to your specific use case, but still – here's how you could send the PDF using Craft's <a href="https://craftcms.com/classreference/services/EmailService#sendEmail-detail" rel="nofollow noreferrer">EmailService</a>:</p>
  
  <pre><code>$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);
  </code></pre>
  
  <p>(If amforms doesn't offer a way to include the PDF attachment, you might be able to solve it by a) using the <code>onBeforeEmailSubmission</code> event instead and setting <code>$event-&gt;performAction</code> to <code>false</code> inside the event handler, to prevent amforms from actually sending the email confirmation; then compile an <code>EmailModel</code> yourself (like in the example above), and sending that instead – just an idea)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-09-10T22:18:39.030Z
Answers.Text: wowowow, this answer goes above and beyond! Thanks for all the details, I'll try giving it ago sometime this week and getting back to you.
Answers.UserDisplayName: Ryan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-30T13:33:27.817Z
Answers.Text: The hook works great, but I can't seem to get the actual submission out of the event that is passed...
Answers.UserDisplayName: Mark Buikema
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-27T11:32:10.343Z
Answers.Text: this should be marked as the solution.
Answers.UserDisplayName: Hal
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-09-08T23:28:36.863Z
Answers.LastActivityDate: 2017-09-08T23:53:28.153Z
Answers.LastEditDate: 2017-09-08T23:53:28.153Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.Body: <p>Here is a working example using mPDF 6.1.  Thanks to Mats Mikkel Rummelhoff for pointing me in the right direction.  </p>
  
  <p>Note: the event you need is amForms_submissions.onBeforeEmailConfirmSubmission. This event contains the confirmation email, so you can add your attachment straight to it. </p>
  
  <pre><code>public function init(){
      require_once __DIR__ . '/vendor/autoload.php';
      //If you want to attach to the submission email, use: onBeforeEmailSubmission
      craft()-&gt;on('amForms_submissions.onBeforeEmailConfirmSubmission', function (Event $event) {
  
          //Grab the params out of the event. From this you can get the submission and the associated form.
          $params = $event-&gt;params;
          $submission = $params['submission'];
          $form = $submission-&gt;getForm();
  
          //This bit is pretty much straight out of the getSubmissionEmailBody method in the AMForms_SubmissionsServerice
          $variables = array(
                  'tabs' =&gt; $form-&gt;getFieldLayout()-&gt;getTabs(),
                  'form' =&gt; $form,
                  'submission' =&gt; $submission
              );
          //Get HTML using template. The default amforms email template is a good starting point and can be found in: craft/plugins/amforms/templates/_display/templates/email.twig
          $html = craft()-&gt;templates-&gt;render('_amforms/attachment', $variables);
  
          //Create the mPDF object.
          $mpdf = new \mPDF();
          //Feed in the HTML.
          $mpdf-&gt;WriteHTML($html);
          //Output the PDF as a string.
          $data = $mpdf-&gt;Output('', 'S');
          //Grab the EmailModel from the event params.
          $email = $params['email'];
          //Add the PDF string as an attachment and specify a file name.
          $email-&gt;addStringAttachment($data,'Email Attachemnt.pdf');
      });
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-11T12:03:23.393Z
Answers.LastActivityDate: 2018-05-11T12:03:23.393Z
Answers.UserDisplayName: Druin
Body: <p>I'm using <a href="https://github.com/am-impact/amforms" rel="nofollow noreferrer">amforms</a> for a client and need to create a user confirmation email that has a pdf attached containing the submitted form data, like a receipt.</p>
  
  <p>After a little google searching, I believe I'd have to hook into amforms' <code>onEmailSubmission</code> method and generate the pdf using the <a href="http://www.mpdf1.com/mpdf/index.php" rel="nofollow noreferrer">mPDF library</a>, is this correct?</p>
  
  <ol>
  <li>How do I add the mPDF library to Craft?</li>
  <li>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?</li>
  <li>And then finally attach that pdf to the user confirmation email?</li>
  </ol>
  
  <p>Does anybody have experience with this whether using amforms or not?</p>
  
Tags: <form><plugin-amforms><pdf>
Title: Generate pdf with form submission data
ThreadText: # Generate pdf with form submission data
  
  (Asked by: username_0 on 2017-09-08)
  
  I'm using [amforms](https://github.com/am-impact/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](http://www.mpdf1.com/mpdf/index.php), is this correct?
  
  1. How do I add the mPDF library to Craft?
  2. 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?
  3. And then finally attach that pdf to the user confirmation email?
  
  Does anybody have experience with this whether using amforms or not?
  
  **Tags:** form, plugin-amforms, pdf
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-09-08. Score: 4
  
  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](http://pluginfactory.io) a [custom plugin](https://craftcms.com/docs/plugins/introduction), 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](https://getcomposer.org/) (which mPDF [supports](https://github.com/mpdf/mpdf#installation)) 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()->on('amForms_submissions.onEmailSubmission', function (Event $event) {
  
          $params = $event->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()->templates->render('/path/to/template', $params);
  
          // Create a mPDF instance, and generate a PDF file from the HTML
          $mpdf = new Mpdf();
          $mpdf->WriteHTML($html);
  
          // Have mPDF save the PDF file to some location
          $mpdf->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](https://craftcms.com/classreference/services/EmailService#sendEmail-detail):
  
  ```
  $email = new EmailModel();
  $email->toEmail = 'hello@example.com';
  $email->subject = 'Your PDF is ready, boss';
  $email->body    = 'How you doin, {{ user.name }}?';
  $email->attachments = [[
     'path' => '/path/to/your/pdf.pdf',
     'name' => 'An awesome PDF',
     'encoding' => 'UTF-8',
     'type' => 'application/pdf',
  ]];
  
  craft()->email->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->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)
  
  > **Comments:**
  > **username_0**: wowowow, this answer goes above and beyond! Thanks for all the details, I'll try giving it ago sometime this week and getting back to you.  
  > **username_2**: The hook works great, but I can't seem to get the actual submission out of the event that is passed...  
  > **username_3**: this should be marked as the solution.  
  
  ---
  
  ### Answer by username_4 on 2018-05-11. Score: 3
  
  Here is a working example using mPDF 6.1. Thanks to username_1 for pointing me in the right direction.
  
  Note: the event you need is amForms\_submissions.onBeforeEmailConfirmSubmission. This event contains the confirmation email, so you can add your attachment straight to it.
  
  ```
  public function init(){
      require_once __DIR__ . '/vendor/autoload.php';
      //If you want to attach to the submission email, use: onBeforeEmailSubmission
      craft()->on('amForms_submissions.onBeforeEmailConfirmSubmission', function (Event $event) {
  
          //Grab the params out of the event. From this you can get the submission and the associated form.
          $params = $event->params;
          $submission = $params['submission'];
          $form = $submission->getForm();
  
          //This bit is pretty much straight out of the getSubmissionEmailBody method in the AMForms_SubmissionsServerice
          $variables = array(
                  'tabs' => $form->getFieldLayout()->getTabs(),
                  'form' => $form,
                  'submission' => $submission
              );
          //Get HTML using template. The default amforms email template is a good starting point and can be found in: craft/plugins/amforms/templates/_display/templates/email.twig
          $html = craft()->templates->render('_amforms/attachment', $variables);
  
          //Create the mPDF object.
          $mpdf = new \mPDF();
          //Feed in the HTML.
          $mpdf->WriteHTML($html);
          //Output the PDF as a string.
          $data = $mpdf->Output('', 'S');
          //Grab the EmailModel from the event params.
          $email = $params['email'];
          //Add the PDF string as an attachment and specify a file name.
          $email->addStringAttachment($data,'Email Attachemnt.pdf');
      });
  }
  
  ```
  
  
  ---
  
  