Introduction #
In one of my recent projects, client needed contact form which would send confirmation message to person submitting form. This message was supposed to have pdf attachment generated from submitted data.
Here's what's needed to build such functionality:
- First, we need the contact form. I decided to use advanced form builder plugin Freeform.
- Right before the form is sent, we need to grab submission data.
- Using this data, we need to dynamically generate PDF file.
- Finally, we need to attach generated PDF file to submission and send it.
Because we will be working with emails, I recommend using mailtrap for testing.
Attaching functionality to the event #
First things first - creating the contact form. This is actually described pretty extensively in Freeform docs, so I will skip that part.
Our code will live in PHP module. Every Craft install starts with empty module - we just need to enable it in config/app.php
. You can find more info about modules in Enhancing a Craft CMS 3 Website with a Custom Module article on nystudio blog.
We will run our custom code within Freeform EVENT_BEFORE_SEND event - it will allow us to grab message contents right before it is sent.
Place this code before module class code starts:
use Solspace\Freeform\Services\MailerService;
use Solspace\Freeform\Events\Mailer\SendEmailEvent;
use yii\base\Event;
Place this code in init()
method of module class:
Event::on(
MailerService::class,
MailerService::EVENT_BEFORE_SEND,
function (SendEmailEvent $event) {
$formHandle = 'someForm';
if($event->getForm()->getHandle() != $formHandle){
return false;
}
});
Note how we check for form handle (someForm
in our case), to verify if we are dealing with our specific form. Its important to perform such checks to prevent any unexpected bahaviour.
Genrating PDF with mPDF library #
To generate PDF file, we will use mpdf library. Install it with composer:
composer require mpdf/mpdf
We will use Twig templates to render HTML which will be used to generate our PDF document.
$variables = [
'fields' => $event->getFieldValues()
];
$html = Craft::$app->getView()->renderTemplate('_emails/pdf_template.twig', $variables, Craft::$app->view::TEMPLATE_MODE_SITE);
$htmlHeader = Craft::$app->getView()->renderTemplate('_emails/pdf_header.twig', $variables, Craft::$app->view::TEMPLATE_MODE_SITE);
$htmlFooter = Craft::$app->getView()->renderTemplate('_emails/pdf_footer.twig', $variables, Craft::$app->view::TEMPLATE_MODE_SITE);
This code will look for _emails/pdf_template
, _emails/header.twig
, _emails/footer.twig
in your templates directory. Separate files are used for main document body (which can be many pages long), footer and header (which are repeated for each page). getFieldValues()
Freeform method is used to pass submission data into template context, as fields
array containing form field values. You can also use other Freeform methods and properties to pass various submission and form data to Twig.
Here's a very simple example Twig template used to render PDF body:
Sender first name: {{fields.firstNameHandle}} <br>
Sender surname: {{fields.surnameHandle}} <br>
You can display page number in footer template by using {PAGENO}
keyword. Note - this is not a Twig variable, but something mPdf specific.
Current page: {PAGENO}
Keep in mind that HTML tags and CSS styles that are accepted by mPDF are kinda restricted. For example, you cannot use flexbox. On the other hand, there are some custom HTML tags used by this library. For example, to create two column layout, you can use custom
Once we render HTML markup, we can finally feed it into mPDF.
$pdf = new \Mpdf\Mpdf([
'margin_top' => 30,
'margin_left' => 15,
'margin_right' => 15,
'margin_bottom' => 30,
]);
$pdf->SetHTMLHeader($htmlHeader);
$pdf->SetHTMLFooter($htmlFooter);
$pdf->WriteHTML($html);
$pdf = $pdf->Output('', \Mpdf\Output\Destination::STRING_RETURN);
This code created PDF file, applied some margins to it and injected HTML content into document. Now, we need to attach our file to form submission.
$message = $event->getMessage();
$fileName = 'attachment.pdf';
$message->attachContent($pdf, [
'fileName' => $fileName,
'contentType' => 'application/pdf',
]);
That's it - now our form email will have PDF attachment. Here's whole module code to copy and paste into your project:
Event::on(
MailerService::class,
MailerService::EVENT_BEFORE_SEND,
function (SendEmailEvent $event) {
// get specific form
$formHandle = 'someForm';
if($event->getForm()->getHandle() != $formHandle){
return false;
}
// render pdf HTML
$variables = [
'fields' => $event->getFieldValues()
];
$html = Craft::$app->getView()->renderTemplate('_emails/pdf_template.twig', $variables, Craft::$app->view::TEMPLATE_MODE_SITE);
$htmlHeader = Craft::$app->getView()->renderTemplate('_emails/pdf_header.twig', $variables, Craft::$app->view::TEMPLATE_MODE_SITE);
$htmlFooter = Craft::$app->getView()->renderTemplate('_emails/pdf_footer.twig', $variables, Craft::$app->view::TEMPLATE_MODE_SITE);
// create pdf file
$pdf = new \Mpdf\Mpdf([
'margin_top' => 30,
'margin_left' => 15,
'margin_right' => 15,
'margin_bottom' => 30,
]);
$pdf->SetHTMLHeader($htmlHeader);
$pdf->SetHTMLFooter($htmlFooter);
$pdf->WriteHTML($html);
$pdf = $pdf->Output('', \Mpdf\Output\Destination::STRING_RETURN);
// add pdf file to submission email
$message = $event->getMessage();
$fileName = 'attachment.pdf';
$message->attachContent($pdf, [
'fileName' => $fileName,
'contentType' => 'application/pdf',
]);
});
Dynamic PDF attachment with other plugins #
If you want, you can easily adapt this code to work witth other contact from plugins, like Express form, Formie or even Contact form. You just need to use specific event emitted by that plugin and some method to add attachment to message that is being sent.
Using other PDF generation libraries #
If mPDF is causing you problems for some reason, you can try other libraries. For example - dompdf, which is actually used by Craft Commerce to generate PDF invoices.
Using the Craft CMS plugin #
If you need ready to use solution that will let you render PDF file that can be downloaded when browsing Craft website, you can use PDF Generator plugin.