Intuition

A client of mine requested the feature in their existing webform (on a Drupal10 website) to be able to generate an PDF print-out of the user’s input upon their every submission, and attach the generated PDF to the with the e-mail trigger by the webform’s handler.

2025-10-29T113851

This will ensure they have a copy of the user’s input as the ground truth to be referenced in the future, and the signature in the webform can be shared via the email as a part of the PDF, because by default the webform only allows you to attach the link of signature’s PNG image, which means (if you want any user who receive the email to be able to download the signature without a Drupal authenticated account) you will have to expose via a public link.


Steps to Achieve

Step-1: Module Required

To begin with, install the following modules:

  • To generate PDF from the webform submission:

    • Entity: provides the Entity API (drupal/entity)

    • Entity Print: allows you to print any Drupal entity or View to PDF (drupal/entity_print)

    • Webform Entity Print (PDF): allows site builders to download, export, and email PDF copies of webform submissions (submodule of drupal/webform)

  • To be able to attach files (i.e. attachment) in the email:

    • MailSystem: provides an Administrative UI for managing the used mail backend/plugin. (drupal/mailsystem)

    • MimeMail: provides a simple API for adding attachments to email. (drupal/mimemail)

    • Webform Entity Print (PDF) Attachment: provides attachment integration in webform handler. (submodule of drupal/webform)

In short you can execute the following code snippet:

1
2
3
4
5
composer require 'drupal/mailsystem'
composer require 'drupal/mimemail'
composer require 'drupal/entity'
composer require 'drupal/entity_print'
composer require drupal/webform       # Usually when you reach  this step you will already have webform installed...
1
2
3
4
5
6
drush pm:install mailsystem
drush pm:install mimemail
drush pm:install entity
drush pm:install entity_print
drush pm:install webform_entity_print
durhs pm:install webform_entity_print_attachment

Step-2: Create a Demo Webform

For demo purpose I have created a webform containing several text field, an email field, a document document (file) field and a signature field :

2025-10-29T131451

Here’s the source YML file for your convenience: link

(Note: when adding fields in the webform, if you are not seeing the “file”, “document”, “image” option, you may have not set you Drupal’s file path in settings.php, e.g. $settings['file_private_path']='sites/default/files/private')

In order for your website to be able to send files through attachment, use “Mime Mail mailer” as the sender. Using “Mime Mail mailer” as the formatter is optional, but in general I much prefer the output of Mime mailer (The output of “Default PHP mailer” looks a bit too primitive / plain-text to me….)

2025-10-29T130550

Step-4: Add Attachment PDF Field(s)

To begin with, with the help from “Webform Entity Print (PDF)” and “Webform Entity Print (PDF) Attachment” modules, a new element/field type called “Attachment PDF” will be available in your webform’s build:

2025-10-29T132636

For this additional “Attachment PDF” element/field, you can either:

  • Choose to use “View mode: default” such that everything in the webform just prints out in the same order/sequence as the webform’s build
  • Choose to use “View mode: twig template” such that you can customise what gets hidden and what’s gets shown and in which format.

Moreover, you can use webform token in the filename, for instance submission_signature-[current-date:raw].pdf:

2025-10-29T132907

Finally, you’ll need to create a “email/handler” for the webform, please note that at the bottom of its settings, you need to check the toggle under “Attachments > Include files as attachments”. Without it, the email won’t have any attachments !

2025-10-29T134708

Step-6: Test Webform Submission

Finally use the webform’s test feature to lodge a example submission to trigger a email send-out: (here I’m using ddev with mailpit to check out the emails, and had the twig debug turned on)

2025-10-29T135330

You can also use the debug feature of the email/handler to checkout the submitted email:

2025-10-29T135659

Step-7: Attachment PDF Style

(PENDING) I don’t really have too much time to figure this out, but as far as I know, there are at least two methods:

  • Method-1: the styling for the generated PDF attachment is coming from this file: css/webform_entity_print.css in the webform_entity_print module, see below screenshot to showcase overriding it to add border taking effect on the final outcome

    2025-10-29T140432

  • Method-2: you can customise the pdf style via the <style> tag of the “attachment pdf” field:

    2025-10-29T140938

(there’s definitely a more graceful way…. this links maybe helpful for my/your future reading: link-1):


Reference